1

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

Community
  • 1
  • 1
toti
  • 27
  • 1
  • 6

4 Answers4

3

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.

Chris Disley
  • 1,286
  • 17
  • 30
  • +1 for "scripting the database" ,It reduces the DB size and I think itz better. – DayTimeCoder May 29 '12 at 20:20
  • I found this, good one: http://stackoverflow.com/questions/9734816/quick-deployment-of-visual-studio-2010-app-with-sql-database – toti May 29 '12 at 20:23
  • If you're looking for a way to bootstrap the install of SQL server I believe there's a merge module for this inside the VS install directory that you can import into your setup project. – Chris Disley May 29 '12 at 20:26
  • That's SQL express btw. Full SQL server install would of course require licenses etc. – Chris Disley May 29 '12 at 20:27
2

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.

user653649
  • 115
  • 1
  • 9
0

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.

Gaurav Gandhi
  • 3,041
  • 2
  • 27
  • 40
0

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'"
David Brabant
  • 41,623
  • 16
  • 83
  • 111