验证一个有效的SQL字符串字符串、有效、SQL

2023-09-04 12:28:33 作者:无痕.

有没有一种方法(或现有的库,不一定内置到.NET)在C#中,执行简单的SQL字符串验证?

Is there a way (or existing library, not necessarily built into .NET) in C# that performs simple SQL string validation?

场景:建筑update语句,以减少对SQL负载与个别报表的负载。如果字符串构建做了一些奇怪,像一个逗号,例如结束,我希望能够以验证字符串正确。

Scenario: Building update statement to reduce load on SQL load vs. individual statements. In case the string build does something "odd", like ending with a comma for instance, I'd like to be able to validate that the string is correct.

推荐答案

如果你想使用SQL Server验证SQL,您可以通过添加这样做

If you want to validate the SQL using SQL Server, you can do so by adding

SET PARSEONLY ON

你的脚本之前,然后

before your script, and then

SET PARSEONLY OFF

在你的脚本。

如果你需要避免去到数据库,那么也许你可以使用该程序集 Microsoft Visual Studio团队系统2008年数据库版GDR 包括(他们有code处理SQL解析和脚本生成)。如果你没有数据库版本或团队套件,你可以下载试用版本,以获得该组件。

If you need to avoid going to the database, then maybe you can use the assemblies that Microsoft Visual Studio Team System 2008 Database Edition GDR includes (they have code that handles SQL parsing and script generation). If you don't have the database edition or team suite, you can download the trial version to get the assemblies.

这里是一个链接我发现如果有人是使用这些程序集。

Here is a link I found where someone is using these assemblies.