繁体   English   中英

作为Visual Studio 2010安装项目的一部分运行SQL脚本

[英]Running SQL script as part of visual studio 2010 Setup Project

美好的一天,

我的目标是为Windows服务创建一个msi,在安装过程中必须运行各种sql脚本。

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

我可以使用msdn概述为服务添加创建安装项目

我的问题是我不知道如何在安装过程中运行sql脚本(以及如何指定sql连接)

有没有办法在visual studio中执行此操作,还是需要使用批处理文件/购买InstallShield?

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

编辑 :我正在使用visual studio 2010 Professional和SQLServer 2012

您可以在安装过程中运行SQL脚本,就像在任何其他C#应用程序中执行它一样。

您在问题中提到MSDN概述中,提到了在安装过程中编写自己的代码,因此我假设您已经知道如何执行此操作。

有很多方法可以做到这一点。 以下是这里引用的一个例子:

 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(); } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM