我試圖從C#Windows應用程序對數據庫執行腳本(.sql文件)。 SQL文件包含'GO'語句;這意味着我正在使用對象SMO。從C#執行SQL腳本並記錄錯誤
我想繼續錯誤也日誌的任何錯誤可能腳本的數據庫上執行過程中發生的。有沒有辦法做到這一點?
這是我使用的代碼:
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
ServerConnection svrConnection = new ServerConnection(sqlConnection);
Server server = new Server(svrConnection);
string script = File.ReadAllText("upgradeDatabase.sql");
try
{
server.ConnectionContext.ExecuteNonQuery(script, ExecutionTypes.ContinueOnError);
}
catch (Exception ex)
{
//handling and logging for the errors are done here
}
}
任何幫助表示讚賞!
在文本中與** GO **分離可能會破壞腳本,想象一下你必須選擇CATE ** GO ** RY FROM WHATEVER –
您是否注意到我使用將GO固定到行首的正則表達式模式? http://msdn.microsoft.com/en-US/library/az24scfc(v=vs.90) – Steve
謝謝史蒂夫你的解決方法。我正在執行每條單一語句,並在發生時捕獲任何錯誤。 – naregkar