在 C# 中解析 SQL 字符串

发布于 2024-12-05 02:13:38 字数 308 浏览 1 评论 0原文

我需要解析 Command.CommandText

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

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

此字符串将成功。

但是,

"SELECT * FROM SomeTable WHERE (1=1"

不会成功。

现在我的问题。我如何解析这个字符串c#

I have the need to Parse a 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.

Say i have; "SELECT * FROM SomeTable WHERE (1=1)"

This string will succeed.

but,

"SELECT * FROM SomeTable WHERE (1=1"

will not succeed.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只是一片海 2024-12-12 02:13:38

如果您只是想验证语法。您可以使用 Microsoft.Data.Schema.ScriptDom 为此。

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);

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);
月寒剑心 2024-12-12 02:13:38

我尝试过一些旨在解析/操作/生成代码中的 SQL 的库。最近的是ActiveQueryBuilder,但我知道还有很多。

使用 AQB - 您将能够“验证”SQL,但我认为您会遇到的问题是它不是 100%。我使用过的它们都不能提供与实际数据库相同的结果。您会发现某些特定的 SQL 字符串对您的解析器有效,但对数据库无效,反之亦然。例如,在 AQB 中,如果不给它一个别名,就不能有子查询,否则解析器会抛出异常 - 但 Oracle 会很乐意接受并运行相同的 SQL。

根据数据库的不同,您应该能够要求 DATABASE 验证 SQL,而无需运行它。在SQL Server中,我相信你可以使用Prepare语句,在Oracle中我相信它被称为解释计划

这是我发现获得一致结果的唯一方法。当然,如果您的查询预计很简单或者您不需要 100% 的准确性,那么可能会需要更多工作。

I've tried a handful of libraries that are designed to parse/manipulate/generate SQL in code. The most recent was ActiveQueryBuilder, but I know there are many out there.

With AQB - you would be able to 'validate' SQL but the problem I think you'll run into is that it is not 100%. None of them, that I've used, provide identical results to the actual database. You will find some certain SQL string that appears valid to your parser but invalid to the database or vice-versa. For example, in the AQB you couldn't have a subquery without giving it an alias or the parser would throw an exception - but Oracle would gladly accept and run the same SQL.

Depending on the database, you should be able to ask the DATABASE to validate the SQL without running it. In SQL Server, I believe you can use the Prepare statement, in Oracle I believe it's called an Explain Plan.

That's the only way I've found to get consistent results. Of course, if your queries are expected to be simple or if you don't require 100% accuracy it might be more work.

勿忘初心 2024-12-12 02:13:38

基于您使用 SqlCommand 的事实,我假设您想要针对 Sql Server 检查语句的正确性。正如人们所指出的,Prepare 语句是最好的选择。在 c# 方面,您只能检查语法的正确性,而不能检查其他内容(这并不意味着查询会通过)。准备语句将检查命名解析、绑定等,但它将包括到服务器的往返。此外,您必须意识到它的局限性(例如临时对象)。有关 TSQL 准备 语句的详细信息,请查看此处。

Based on the fact that you use SqlCommand, I assume you want to check correctness of the statement against Sql Server. As folks pointed out, Prepare statement is the one to go with. On the c# side you can only check the correctness of the syntax, and nothing else (that does not mean the query will pass). Prepare statement will check naming resolution, binding and more, but it will consist of a round trip to the server. Moreover, you have to be aware of its limitations (ex. temporary objects). For detail on TSQL Prepare statement take a look here.

虫児飞 2024-12-12 02:13:38

正如您所说:“我不想运行查询。”

如果您只需要验证 Microsoft SQL Server 的 SQL 语法,Microsoft.Data.Schema.ScriptDom 是一个不错的选择。否则,您可以考虑其他 SQL 解析器,例如 通用 SQL 解析器(支持 Oracle、SQL Server、DB2、MySQL、Teradata , PostgreSQL) 帮助您在程序中进行离线 SQL 语法检查。

As you said: "I don't want to run the query."

Microsoft.Data.Schema.ScriptDom is a good choice if you only need to validate SQL syntax of Microsoft SQL Server. Otherwise, you may consider other SQL Parser such as General SQL Parser(support Oracle, SQL Server, DB2, MySQL, Teradata, PostgreSQL) help you to do offline SQL syntax check in your program.

等风来 2024-12-12 02:13:38

这可以通过在形式语法的帮助下分析您的查询来完成,但这对您来说可能是一个负担,并且完全取决于您的使用场景。

为了分析任何正式语言,我建议您使用 Irony 库,它在生产和个人项目中都非常流行,并且已经有一个内置的 SQL 语法,因此只需调用该初始化程序即可。它会为您提供有关错误及其类型的信息,但对于更简单的事情,请使用 Ira 的建议。

This can be done by analyzing your query with help of formal grammar but this might be an overload for you and totally depends on your usage scenario.

For analyzing any formal languages I advise you to use Irony library which is very popular both in production and individual projects and already has a built-in SQL grammar, so it would be just a matter of invoking that initializer. It would give you info about errors and their type but for something simpler use Ira's proposal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文