使用C#查询验证 [英] Query Validation using c#

查看:124
本文介绍了使用C#查询验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个在C#中的查询验证,这让我从文本分析SQL文本并把它发在执行前阉其正确与否验证。 (MS SQL或DB2查询)。

I am looking for a query validator in c#, which allows me to parse the SQL Text from textbox and verify wether its correct or not before sending it for execution. (MS SQL or DB2 queries).

推荐答案

如果您想验证SQL语法不使用数据库时,<一个HREF =http://msdn.microsoft.com/en-us/library/microsoft.data.schema.scriptdom.sql.tsql100parser.aspx> TSql100Parser 类将针对这种情况做的很好。

If you want to validate SQL syntax without the use of a database, the TSql100Parser class will do well for this situation.

免责声明,从这篇文章借用这里的代码的代码来验证SQL脚本

Disclaimer, code borrowed from this post here Code to validate SQL Scripts

相当简单的,虽然使用。如果返回null,则有在分析它是没有错误

Pretty straightforward to use though. If it returns null, then there were no errors in parsing it.

using Microsoft.Data.Schema.ScriptDom;
using Microsoft.Data.Schema.ScriptDom.Sql;

public class SqlParser
{
        public List<string> Parse(string sql)
        {
            TSql100Parser parser = new TSql100Parser(false);
            IScriptFragment fragment;
            IList<ParseError> errors;
            fragment = parser.Parse(new StringReader(sql), out errors);
            if (errors != null && errors.Count > 0)
            {
                List<string> errorList = new List<string>();
                foreach (var error in errors)
                {
                    errorList.Add(error.Message);
                }
                return errorList;
            }
            return null;
        }
}

这篇关于使用C#查询验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆