toti
toti

Reputation: 27

How to create a setup file for a project that is connected to SQL database?

As mentioned in the title of the question; I have a desktop application which is visual studio project that is connected to SQL database. I want to create an exe file (setup file) for this project, how can I attach the database with it?

Thanks.

Same problem with solution: Quick deployment of Visual Studio 2010 app with SQL database

Upvotes: 1

Views: 8519

Answers (4)

David Brabant
David Brabant

Reputation: 43589

You can use xcopy deployment with SQL Server Express databases. From the link mentioned:

To make your application work with the Xcopy deployment feature of SQL Server Express, you must make sure that the connection string you use in your application contains the appropriate parameters:

Use the data source parameter, but change the computer name to either a period (.) or (local). You must also specify the name of the instance, unless you are sure that SQL Server Express will always be installed on an unnamed instance.

Use the initial catalog or database parameter, but do not set the parameter to a value.

Add the AttachDBFileName parameter and set it to the name and path of the .mdf file. Attachdbfilename is a SqlClient connection string option that enables attaching databases at runtime and autogenerates database name. The DataDirectory keyword lets you specify the relative path of a database file. Attachdbfilenamealso helps with database portability. For more information about Attachdbfilename, see the Visual Studio 2005 documentation.

The following connection string will attach the MyDb.mdf database file, which is in the same folder as the application executable, to the SQL Server Express instance running on the local computer.

@"Data Source='.\SQLExpress'; Initial Catalog=; Integrated 
Security=true; AttachDBFileName='" |DataDirectory| + 
@"\MyDb.mdf'"

Upvotes: 0

Gaurav Gandhi
Gaurav Gandhi

Reputation: 3201

Make your database during Setup only, by Running Scripts for creation and intialization if needed
Use custom Scripts, and Custom Dialog Box to Achieve this.

Upvotes: 0

user653649
user653649

Reputation: 115

Add the MDF to your project as a content file. The installer will copy content files to the install folder, so then it's just a problem of correctly using that database in the application. SMO might be able to help out there.

Upvotes: 2

Chris Disley
Chris Disley

Reputation: 1355

If it's SQL server, I'd suggest scripting the database to a file, and use a custom action to call osql with that script against a database server specified in a variable.

Upvotes: 3

Related Questions