2015-02-23 115 views
0

好日子的一部分,運行SQL腳本Visual Studio 2010的安裝項目

我的目標是創建一個MSI的Windows服務和安裝各種SQL腳本必須運行過程中。

我想要做的是創建一個msi,當用戶運行它時,它將在pc上安裝服務並運行3個.sql腳本,一個用於創建數據庫,一個用於填充數據庫,另一個用於添加存儲過程。

我可以添加創建使用the msdn overview

我的問題的服務安裝項目是,我不知道如何在安裝過程中(以及如何指定SQL連接)

運行SQL腳本

有什麼辦法可以在Visual Studio中做到這一點,或者我需要訴諸批處理文件/購買InstallShield嗎?

任何幫助表示讚賞,
問候
傑夫

編輯:我使用Visual Studio 2010專業版和SQLServer 2012

回答

0

您可以在安裝完全相同的方式在運行SQL腳本你會在任何其他C#應用程序中執行它。

MSDN overview you've mentioned in the question中,提到在安裝過程中編寫自己的代碼,因此我假設您已經知道如何執行該操作。

有很多方法可以做到這一點。這裏只是一個例子出許多,從here引:

string queryString = "SELECT tPatCulIntPatIDPk, tPatSFirstname, tPatSName, tPatDBirthday FROM [dbo].[TPatientRaw] WHERE tPatSName = @tPatSName"; 
string connectionString = "Server=.\PDATA_SQLEXPRESS;Database=;UserId=sa;Password=2BeChanged!;"; 

using (SqlConnection connection = new SqlConnection(connectionString)) 
{ 
    SqlCommand command = new SqlCommand(queryString, connection); 
    command.Parameters.AddWithValue("@tPatSName", "Your-Parm-Value"); 
    connection.Open(); 
    SqlDataReader reader = command.ExecuteReader(); 
    try 
    { 
     while (reader.Read()) 
     { 
      Console.WriteLine(String.Format("{0}, {1}", 
      reader["tPatCulIntPatIDPk"], reader["tPatSFirstname"]));// etc 
     } 
    } 
    finally 
    { 
     // Always call Close when done reading. 
     reader.Close(); 
    } 
}