code,以验证SQL脚本脚本、code、SQL

由网友(被我吃掉了)分享简介:我如何可以验证使用.NET 2.0和C#执行之前的SQL脚本? How can I validate sql scripts before executing them using .net 2.0 and c#? 如果在SQL是无效的我要回错误行。If the sql is not valid I want t...

我如何可以验证使用.NET 2.0和C#执行之前的SQL脚本?

How can I validate sql scripts before executing them using .net 2.0 and c#?

如果在SQL是无效的我要回错误行。

If the sql is not valid I want to return error rows.

推荐答案

如果您正在创建一个工具,允许用户输入一些SQL code。通过手,要验证code输入使用C#$ C $Ç执行的SQL服务器上之前,你可以创建一个方法是这样的:

If you are creating a tool that allows the user enter some sql code by hand and you want to validate the code entered using C# code before execution on sql server, you can create a method like this:

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;
        }
}
阅读全文

相关推荐

最新文章