.NET 前端中的 SQL 存储过程验证(类似于 SSMS 中的解析选项) [英] SQL Stored Procedure Validation in .NET Front End (Similar to Parse Option in SSMS)

查看:17
本文介绍了.NET 前端中的 SQL 存储过程验证(类似于 SSMS 中的解析选项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的前端应用程序有一个文本区域,用户可以在其中输入存储过程.我想验证 SP,类似于选项解析"在 SSMS (Ctrl+F5) 中.

My front end application has a textarea where the user can enter the stored procedure. I want to validate the SP, similar to the option "Parse" in SSMS (Ctrl+F5).

我们有图书馆吗?或功能?完成这个?

Do we have any Library? or Function? to accomplish this?

提前致谢.

推荐答案

你可以使用 Microsoft.SqlServer.SqlManagementObjects 或 (SMO) 简称

You could use Microsoft.SqlServer.SqlManagementObjects or (SMO) in short

你需要打电话

var options = new Microsoft.SqlServer.Management.SqlParser.Parser.ParseOptions
{
  BatchSeparator = "GO",
  CompatibilityLevel = Microsoft.SqlServer.Management.SqlParser.Common.DatabaseCompatibilityLevel.Version140 // depedning on the compatibilty level of your database.
};
var output = Microsoft.SqlServer.Management.SqlParser.Parser.Parser.Parse(inputString, options);

if(output.Errors.Any())
{
   // handle errors
}

这与执行 Parse 命令时从 SSMS 执行的代码相同.

This is the same code that is executed from SSMS when you execute the Parse command.

为什么不应该在 SQL 服务器上执行此操作:

Why you shouldn't do this on a SQL server:

  • SQL 注入
  • 不要依赖 SQL Server
  • 不要用不适合 SQL Server 的东西烧掉宝贵的 SQL Server CPU.(SQL Server 是基础架构中昂贵的一部分,在 C# 中运行它的成本要低得多.)
  • 使用自定义兼容性级别,独立于 SQL Server 配置.
  • options.TransactSqlVersion 如果要解析 Azure SQL,也可以使用
  • SQL Injection
  • Don't be dependent on a SQL Server
  • Don't burn precious SQL Server CPU with things that are not ment for SQL server. (SQL server is an expensive part of your infrastructure running it in C# is way cheaper.)
  • Use custom compatibility levels, independent of the SQL Server configuration.
  • options.TransactSqlVersion could also be used if you want to parse for Azure SQL

这篇关于.NET 前端中的 SQL 存储过程验证(类似于 SSMS 中的解析选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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