简体   繁体   中英

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.

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.

I am able to add create a setup project for the service using the msdn overview

My problem is that I have no idea how to run a sql script during the install (and how to specify the sql connection)

Is there any way to do this in visual studio, or will I need to resort to batch files/purchasing InstallShield?

Any help is appreciated,
Regards
Jeff

EDIT : I am using visual studio 2010 Professional and SQLServer 2012

You can run the SQL script during the installation exactly the same way you'd do it in any other C# application.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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