解析在C#中的SQL字符串 [英] Parsing a SQL string in c#

查看:119
本文介绍了解析在C#中的SQL字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要解析一个 Command.CommandText

我不希望运行的查询。我只是想看看,如果命令执行的查询会成功。

I don't want to run the query. I only want to see if the query will succeed if the command was executed.

说,我有; SELECT * FROM SomeTable WHERE(1 = 1)

这串会成功。

不过,

"SELECT * FROM SomeTable WHERE (1=1"

将不会成功。

will not succeed.

现在我的问题。我将如何解析此字符串 C#

Now my question. How would i Parse this string c#?

推荐答案

如果你只是想验证语法。您可以使用<一个href="http://blogs.msdn.com/b/gertd/archive/2008/08/21/getting-to-the-crown-jewels.aspx">Microsoft.Data.Schema.ScriptDom为了这。

If you just want to validate the syntax. You can use Microsoft.Data.Schema.ScriptDom for this.

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

.....

        string sql = "SELECT * FROM SomeTable WHERE (1=1";
        var p = new TSql100Parser(true);
        IList<ParseError> errors;

        p.Parse(new StringReader(sql), out errors);


        if (errors.Count == 0)
            Console.Write("No Errors");
        else
            foreach (ParseError parseError in errors)
                Console.Write(parseError.Message);

这篇关于解析在C#中的SQL字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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