Deploy SQL Database for VB.NET Application

I have read many articles here, but have not found a solution. I have a SQL Server Express database that is used by my VB.NET application. I have packaged and deployed the application through the MSI file and everything works fine, except that I cannot figure out how to include my database file in the package. I understand that there are three general ways to do this (manually copy files, user actions, and SQL scripts). I did not need anything interesting here, just a quick way to put the database on the client machine so that my application can access it.

I decided that copying over the database was the fastest option. I tried to put it in the working directory and in the \DATA directory of the SQL Server Express client installation, but my application did not connect. I also tried changing my connection in the project to .\SQLEXPRESS instead of [my_computer_name]\SQLEXPRESS , followed by rebuilding the deployment project and reinstalling on the client machine, but without soup for me. Same question. I tried to change the "UserInstance" property in the project to "True", but my project did not allow me to save this action.

Will I fix that a manual copy is the fastest and easiest way to do this?

0
source share
2 answers

You must attach your file to the Sql server instance.

 CREATE DATABASE YourDatabaseName ON (FILENAME = 'C:\your\data\directory\your_file.mdf'), (FILENAME = 'C:\your\data\directory\your_file_Log.ldf') FOR ATTACH; 
+2
source

You need to attach your database file to the running SQL Server on the client machine. This can be easily done using this option in the connection string stored in your configuration file (app.config or web.config)

 Server=.\SQLExpress;AttachDbFilename=where_you_have_stored_the_mdf_file; Database=dbname; Trusted_Connection=Yes; 

you can use the substitution string |DataDirectory| .
This shortcut eliminates the need for hard coding the full path.
Using DataDirectory, you can have the following connection string:

 Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\yourfile.mdf" Database=dbname; Trusted_Connection=Yes; 
0
source

Source: https://habr.com/ru/post/1203213/


All Articles