简体   繁体   English

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

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

Good day, 美好的一天,

My goal is to create a msi for a windows service and during installation various sql scripts must run. 我的目标是为Windows服务创建一个msi,在安装过程中必须运行各种sql脚本。

What I want to do is create a msi that, when the user runs it, it installs the service on the pc and runs 3 .sql scripts, one for creating the database, one for population, and one to add a stored procedure. 我想要做的是创建一个msi,当用户运行它时,它会在pc上安装服务并运行3个.sql脚本,一个用于创建数据库,一个用于填充,一个用于添加存储过程。

I am able to add create a setup project for the service using the msdn overview 我可以使用msdn概述为服务添加创建安装项目

My problem is that I have no idea how to run a sql script during the install (and how to specify the sql connection) 我的问题是我不知道如何在安装过程中运行sql脚本(以及如何指定sql连接)

Is there any way to do this in visual studio, or will I need to resort to batch files/purchasing InstallShield? 有没有办法在visual studio中执行此操作,还是需要使用批处理文件/购买InstallShield?

Any help is appreciated, 任何帮助表示赞赏,
Regards 问候
Jeff 杰夫

EDIT : I am using visual studio 2010 Professional and SQLServer 2012 编辑 :我正在使用visual studio 2010 Professional和SQLServer 2012

You can run the SQL script during the installation exactly the same way you'd do it in any other C# application. 您可以在安装过程中运行SQL脚本,就像在任何其他C#应用程序中执行它一样。

In the MSDN overview you've mentioned in the question , there is reference to writing your own code during the installation, so I'll assume you already know how to do that. 您在问题中提到MSDN概述中,提到了在安装过程中编写自己的代码,因此我假设您已经知道如何执行此操作。

There are many ways to do this. 有很多方法可以做到这一点。 Here's just one example out of many, quoted from 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(); } } 

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

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