劇本我都在SQL Server 2008中.sql文件,我想執行對我的數據庫的數據庫腳本。相反,這是一個手動練習,我想將它自動化。我有3個選項可用於我,批處理文件,PowerShell或C#。我執行此操作的方式會受到我下一個問題的影響 - 我想執行腳本,但是如果腳本因任何原因無法執行,請在代碼中注意這一點?這將成爲一組常規安裝步驟的一部分,因此如果腳本無法正確執行,希望安裝停止。執行對SQL Server數據庫
回答
- 通過sqlcmd.exe引導程序運行 - 更難處理錯誤
- 通過自己的自定義應用程序中運行 - 處理錯誤,併發出警報
它是安裝過程的一部分,所以這些選項都不適用。 – amateur
更正的答案 –
你看着使用SQLCMD實用工具最簡單的方法? http://msdn.microsoft.com/en-us/library/ms162773.aspx
以下是通過安裝在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
寫下來的數據庫連接和腳本執行代碼在上述事件中。如果安裝程序未能執行腳本,則異常將到來,並且控件將自動移至回滾事件。所以最後回滾事件會告訴用戶由於執行腳本失敗而無法安裝...
您可以嘗試SQL Server管理對象(SMO - http://msdn.microsoft.com/en-us/library/ms162169.aspx),這應該在您的方案中起作用,在安裝過程中執行sql腳本。
在這個簡短的代碼,你可以看到如何連接到SQL Server實例,這裏使用Windows身份驗證,但你也可以通過使用您的憑據提供connection.Login和connection.Password使用SQL身份驗證。 之後的事件處理程序設置可以在(SERVERMESSAGE,InfoMessage)和火災後(StatementExecuted)腳本執行。同時使用SERVERMESSAGE和InfoMessage這裏是矯枉過正,因爲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;
一批比較容易,我認爲(但這是一個喜好)
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線。
快樂配料
- 1. 在WP7.1 SQL Server CE數據庫上執行SQL查詢
- 2. 如何在AppHarbor SQL數據庫上執行SQL Server作業?
- 3. 如何在SQL Server 2008上執行SQL Server 2005生成的數據庫查詢?
- 4. SQL Server數據庫
- 5. 如何將遠程SQL Server Express數據庫的部分副本執行到本地SQL Server Express數據庫?
- 6. 如何將SQL Server 2008數據庫與SQL Server 2005數據庫進行同步?
- 7. SQL Server - 在SQL Server中執行數據類型的過程
- 8. MSYSGIT BASH:針對MS SQL Server執行SQL
- 9. 無法在SQL Server Management Studio中執行數據庫備份
- 10. SQL Server數據庫在觸發器執行時掛起
- 11. 在多個SQL Server 2008數據庫上自動執行任務
- 12. 在SQL Server 2008中未執行的數據庫腳本
- 13. 在SQL Server中執行sp_databases時排除系統數據庫
- 14. 在SQL Server 2008數據庫上執行搜索
- 15. 如何枚舉SQL Server數據庫中的表並執行DML
- 16. 同步非數據庫SQL Server對象
- 17. 針對SQL Server CE的Northwind數據庫?
- 18. 實現對於SQL Server 2012數據庫
- 19. SQL Server上的SQL Server數據庫或數據庫
- 20. 解析SQL Server查詢而不對數據庫連接執行查詢
- 21. SQL Server 2005對使用「NORECOVERY」恢復的數據庫執行「恢復」
- 22. 如何確定誰在Sql Server數據庫對象上執行DROP/DELETE?
- 23. 使用.MDF SQL Server數據庫與ASP.NET對比使用SQL Server
- 24. 針對大型數據庫的SQL Server數據庫優化
- 25. 對從sql server數據庫使用數據集讀取的數據執行數學運算
- 26. SQL Server數據庫審計爲整個數據庫,所有對象選擇,失敗登錄和執行代碼
- 27. SQL Server:執行存儲過程的一部分在一個數據庫對所有其他數據庫
- 28. SQL:讓SQL對數據庫中執行的一系列操作進行「計數」
- 29. 分離SQL Server數據庫
- 30. 從SQL Server數據庫C#
任何你不使用SQL代理的原因? –
安裝過程的其餘部分如何處理? – NotMe
它目前是一個處理安裝過程的批處理文件,但會作爲對此問題的迴應的一部分進行更改。 – amateur