2012-01-25 74 views
3

劇本我都在SQL Server 2008中.sql文件,我想執行對我的數據庫的數據庫腳本。相反,這是一個手動練習,我想將它自動化。我有3個選項可用於我,批處理文件,PowerShell或C#。我執行此操作的方式會受到我下一個問題的影響 - 我想執行腳本,但是如果腳本因任何原因無法執行,請在代碼中注意這一點?這將成爲一組常規安裝步驟的一部分,因此如果腳本無法正確執行,希望安裝停止。執行對SQL Server數據庫

+0

任何你不使用SQL代理的原因? –

+0

安裝過程的其餘部分如何處理? – NotMe

+0

它目前是一個處理安裝過程的批處理文件,但會作爲對此問題的迴應的一部分進行更改。 – amateur

回答

1

運行它作爲SQL代理作業,你可以設置SQL代理如果腳本未能向您發送警報。

+0

我希望它自動化,作爲安裝過程的一部分,正如問題所述。 – amateur

+0

@amateur,你可以自動化SQL代理作業。可以使用一系列系統特效「sp_add_job」等創建它們。 – Ben

-1

您也可以在存儲過程中收拾這個,有使用一些錯誤處理。

+0

這不能作爲安裝過程的一部分包含在內。 – amateur

-1
  • 通過sqlcmd.exe引導程序運行 - 更難處理錯誤
  • 通過自己的自定義應用程序中運行 - 處理錯誤,併發出警報
+0

它是安裝過程的一部分,所以這些選項都不適用。 – amateur

+0

更正的答案 –

0

以下是通過安裝在c#

代碼在C#中自動執行腳本的進程

using (System.Data.SqlClient.SqlConnection con = new SqlConnection("YourConnection string")) { 
    con.Open(); 
    SqlCommand cmd = new SqlCommand(); 
    string expression = "Parameter value"; 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "Your Stored Procedure"; 
    cmd.Parameters.Add("Your Parameter Name", SqlDbType.VarChar).Value = expression; 
    cmd.Connection = con; 
    using (IDataReader dr = cmd.ExecuteReader()) { 
     if (dr.Read()) { 
     } 
    } 
} 

在你的項目添加一個安裝程序類參考=>Installer class

You can find below mentioned events for the installer process. 
1. Install 
2. Commit 
3. Rollback 
4. UNInstall 

寫下來的數據庫連接和腳本執行代碼在上述事件中。如果安裝程序未能執行腳本,則異常將到來,並且控件將自動移至回滾事件。所以最後回滾事件會告訴用戶由於執行腳本失敗而無法安裝...

0

您可以嘗試SQL Server管理對象(SMO - http://msdn.microsoft.com/en-us/library/ms162169.aspx),這應該在您的方案中起作用,在安裝過程中執行sql腳本。

在這個簡短的代碼,你可以看到如何連接到SQL Server實例,這裏使用Windows身份驗證,但你也可以通過使用您的憑據提供connection.Login和connection.Password使用SQL身份驗證。 之後的事件處理程序設置可以在(SERVERMESSAGEInfoMessage)和火災後(StatementExecuted)腳本執行。同時使用SERVERMESSAGEInfoMessage這裏是矯枉過正,因爲SERVERMESSAGE也將顯示提示性消息(在SQL錯誤的嚴重性< 10),但它是很好的看到它的工作方式。

在此示例中,textBox1.Text包含一個用此行執行的T-SQL腳本: server.ConnectionContext.ExecuteNonQuery(textBox1.Text); 執行語句包圍一個try ... catch執行過程中捕捉任何錯誤。

private void button1_Click(object sender, EventArgs e) 
    { 
     ServerConnection connection = new ServerConnection("stjepan-lap"); 
     connection.LoginSecure = true; 
     Server server = new Server(connection); 
     server.ConnectionContext.InfoMessage += new SqlInfoMessageEventHandler(ConnectionContext_InfoMessage); 
     server.ConnectionContext.StatementExecuted += new StatementEventHandler(ConnectionContext_StatementExecuted); 
     server.ConnectionContext.ServerMessage += new ServerMessageEventHandler(ConnectionContext_ServerMessage); 

     //Executes T-Sql script 
     try 
     { 
      server.ConnectionContext.ExecuteNonQuery(textBox1.Text); 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(ex.Message); 
      if (ex.InnerException != null) 
       Debug.WriteLine(ex.InnerException.Message); 
     } 

     server.ConnectionContext.Disconnect(); 
    } 

    void ConnectionContext_ServerMessage(object sender, ServerMessageEventArgs e) 
    { 
     Debug.WriteLine(e.Error); 
    } 

    void ConnectionContext_StatementExecuted(object sender, StatementEventArgs e) 
    { 
     Debug.WriteLine(e.SqlStatement); 
    } 

    void ConnectionContext_InfoMessage(object sender, SqlInfoMessageEventArgs e) 
    { 
     Debug.WriteLine(e.Message); 
    } 

您應該引用SMO dll文件,並把相應的usings在你的代碼文件

Microsoft.SqlServer.Smo.dll 
Microsoft.SqlServer.ConnectionInfo.dll 
Microsoft.SqlServer.Management.Sdk.Sfc.dll 
.... 
using System; 
using Microsoft.SqlServer.Management.Smo; 
using Microsoft.SqlServer.Management.Common; 
0

一批比較容易,我認爲(但這是一個喜好)

runsqlscr.cmd

@echo off 
REM :: building your script below, not needed if already created 
REM echo Select * from PutTableHere where > %temp%\tmpsql.sql 
REM echo "put rest of sql script here one line at a time" >> %temp%\tmpsql.sql 
REM echo go >> %temp%\tmpsql.sql 
REM echo. 
REM echo script written 
REM echo. 
REM echo ready to execute script 
REM :: delete pause if needed 
REM pause 
REM :: the actual command needed in script 
REM sqlcmd -U thedbuser -P thepasswd -d thedbname -o resultsofscript.txt < %temp%\tmpsql.sql 
REM echo All done, displaying results 
REM type resultofscript.txt 
REM echo. 

如果腳本已經完成,您可以跳過構建腳本,只需更改%temp%tmpsql.sql 到腳本的位置和名稱。

在SQLCMD線

,你需要把自己的價值觀給「*」上述

用於測試,所有你需要爲你的安裝腳本是SQLCMD線。

快樂配料

相關問題