Publish a project with a local database

I created a Windows form application with a local database (.mdf) for storing and retrieving data. The database I'm connecting to: C: \ ProgramData \ Project \ Database.mdf

when I publish my project and place the database file in this folder on another computer and try to run it, I get an error that is unable to find the local installation of the runtime

my connection string:

conn.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\ProgramData\project\Database.mdf"";Integrated Security=True"; 

can anyone help me with this problem? because everything works fine on my own computer

+7
c #
source share
1 answer

Have you included the database in the "Application File"? If you do not do the following (at least this is how I do it):

Project β†’ Properties β†’ Publish β†’ Application Files

Here set the values ​​for your .mdf and xx_log.ldf as follows:

enter image description here

Now, on the Publish tab, go to the Prerequisites section. Here you should check the following depending on which database you are using.

enter image description here

This will download SQL Server Express for the client that installs your application.

You will also have to change the connection string to a common path. I suppose the database is somewhere inside your project / bin folder, I think not sure. Therefore, configure the connection string to something like:

 Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True 

I recommend using a resource file or app.config

But basically, I think your problem is that SQL Server is not installed on the computer on which you are installing. Therefore, simply follow the instructions above in the Prerequisites section. Other steps will allow you to deploy the database to a project folder without manually moving it to a specific folder.

Hope this helps.

+9
source share

All Articles