我正在尝试构建一个程序,该程序以错误处理方式批量执行sql语句(因此,我不使用SMO)。
问题在于 GO 不是SQL的一部分,并且在使用.NET执行语句时,它最终会出现错误(SMO会处理该错误,但不提供执行是否失败的任何指示)。
string statements = File.ReadAllText("c:\\test.sql"); string[] splitted = statements.split("GO");
由于 GO 关键字也可以放在注释中(我不想从语句中删除注释),并且注释可以出现在/ ** /或两个破折号之后,因此使用上述行不能解决我的问题。- 例如,我不希望解析以下代码:
/* GO */
(我用谷歌搜索它,但那边没有解决方案)
最简单(也是最可靠)的解决方案是使用T-SQL解析器。好消息是您不必编写它,只需添加对以下内容的引用:
Microsoft.Data.Schema.ScriptDom
Microsoft.Data.Schema.ScriptDom.Sql
然后使用代码:
static void Main(string[] args) { string sql = @" /* GO */ SELECT * FROM [table] GO SELECT * FROM [table] SELECT * FROM [table] GO SELECT * FROM [table]"; string[] errors; var scriptFragment = Parse(sql, SqlVersion.Sql100, true, out errors); if (errors != null) { foreach (string error in errors) { Console.WriteLine(error); return; } } TSqlScript tsqlScriptFragment = scriptFragment as TSqlScript; if (tsqlScriptFragment == null) return; var options = new SqlScriptGeneratorOptions { SqlVersion = SqlVersion.Sql100, KeywordCasing = KeywordCasing.PascalCase }; foreach (TSqlBatch batch in tsqlScriptFragment.Batches) { Console.WriteLine("--"); string batchText = ToScript(batch, options); Console.WriteLine(batchText); } } public static TSqlParser GetParser(SqlVersion level, bool quotedIdentifiers) { switch (level) { case SqlVersion.Sql80: return new TSql80Parser(quotedIdentifiers); case SqlVersion.Sql90: return new TSql90Parser(quotedIdentifiers); case SqlVersion.Sql100: return new TSql100Parser(quotedIdentifiers); case SqlVersion.SqlAzure: return new TSqlAzureParser(quotedIdentifiers); default: throw new ArgumentOutOfRangeException("level"); } } public static IScriptFragment Parse(string sql, SqlVersion level, bool quotedIndentifiers, out string[] errors) { errors = null; if (string.IsNullOrWhiteSpace(sql)) return null; sql = sql.Trim(); IScriptFragment scriptFragment; IList<ParseError> errorlist; using (var sr = new StringReader(sql)) { scriptFragment = GetParser(level, quotedIndentifiers).Parse(sr, out errorlist); } if (errorlist != null && errorlist.Count > 0) { errors = errorlist.Select(e => string.Format("Column {0}, Identifier {1}, Line {2}, Offset {3}", e.Column, e.Identifier, e.Line, e.Offset) + Environment.NewLine + e.Message).ToArray(); return null; } return scriptFragment; } public static SqlScriptGenerator GetScripter(SqlScriptGeneratorOptions options) { if (options == null) return null; SqlScriptGenerator generator; switch (options.SqlVersion) { case SqlVersion.Sql80: generator = new Sql80ScriptGenerator(options); break; case SqlVersion.Sql90: generator = new Sql90ScriptGenerator(options); break; case SqlVersion.Sql100: generator = new Sql100ScriptGenerator(options); break; case SqlVersion.SqlAzure: generator = new SqlAzureScriptGenerator(options); break; default: throw new ArgumentOutOfRangeException(); } return generator; } public static string ToScript(IScriptFragment scriptFragment, SqlScriptGeneratorOptions options) { var scripter = GetScripter(options); if (scripter == null) return string.Empty; string script; scripter.GenerateScript(scriptFragment, out script); return script; }
添加对以下内容的引用:
Microsoft.SqlServer.Smo
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
然后,您可以使用以下代码:
using (SqlConnection connection = new SqlConnection("Server=(local);Database=Sample;Trusted_Connection=True;")) { ServerConnection svrConnection = new ServerConnection(connection); Server server = new Server(svrConnection); server.ConnectionContext.ExecuteNonQuery(script); }
CodeFluent运行时数据库有一个小的sql文件解析器。它不处理复杂的情况,但是例如支持注释。
using (StatementReader statementReader = new CodeFluent.Runtime.Database.Management.StatementReader("GO", Environment.NewLine, inputStream)) { Statement statement; while ((statement = statementReader.Read(StatementReaderOptions.Default)) != null) { Console.WriteLine("-- "); Console.WriteLine(statement.Command); } }
或更简单
new CodeFluent.Runtime.Database.Management.SqlServer.Database("connection string") .RunScript("path", StatementReaderOptions.Default);