简体   繁体   中英

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

如果是SQL Server,建议将数据库脚本化为文件,然后使用自定义操作针对变量中指定的数据库服务器使用该脚本调用osql。

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.

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.

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'"

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