运行SQL脚本Visual Studio 2010的安装项目

问题描述:

好日子的一部分,运行SQL脚本Visual Studio 2010的安装项目

我的目标是创建一个MSI的Windows服务和安装各种SQL脚本必须运行过程中。

我想要做的是创建一个msi,当用户运行它时,它将在pc上安装服务并运行3个.sql脚本,一个用于创建数据库,一个用于填充数据库,另一个用于添加存储过程。

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

我的问题的服务安装项目是,我不知道如何在安装过程中(以及如何指定SQL连接)

运行SQL脚本

有什么办法可以在Visual Studio中做到这一点,或者我需要诉诸批处理文件/购买InstallShield吗?

任何帮助表示赞赏,
问候
杰夫

编辑:我使用Visual Studio 2010专业版和SQLServer 2012

您可以在安装完全相同的方式在运行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(); 
    } 
}