[英]Ignore error in executing SQL Script
如何在執行SQL Script文件時使代碼忽略錯誤?
如果在執行sql腳本文件時出錯,那么我希望此方法只是忽略它並繼續運行sql腳本文件。 目前,如果我將try-catch塊放在此方法中,則會將錯誤作為異常捕獲,並且在捕獲錯誤后SQL腳本文件的執行將停止。 即使SQL Server中存在錯誤,我也希望繼續執行。
我怎樣才能做到這一點?
我的代碼如下。
private void RunSQLScript()
{
// file is already defined as a SQL Script file
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(ConnectionString);
// split script on GO command
IEnumerable<string> commandStrings = Regex.Split(script, @"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
conn.Open();
foreach (string commandString in commandStrings)
{
if (commandString.Trim() != "")
{
new SqlCommand(commandString, conn).ExecuteNonQuery();
}
}
conn.Close();
}
將ExecuteNonQuery包裝在try / catch塊中,並將catch塊留空。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.