code,以验证SQL脚本 [英] Code to validate SQL Scripts

查看:234
本文介绍了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 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;
        }
}

这篇关于code,以验证SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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