oldsport
oldsport

Reputation: 993

Building a C# WinForms application with a local Db

I am trying to build my first .exe from a c# winforms project. I am using the Flexera Installing Shield. So far I can build and install it and it runs successfully on the same machine where I am developing. In this project I am using a local db. I can also install it on another machine, but as soon as I`m trying to access the Db via a button it complains. I think it has something to do with the connection string. At least it complains at the line where I am trying to access the Db with:

Error 26 - Error Locating Server/Instance Specified 

Here is my obviously wrong connection string:

string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\Users\idiot\Documents\Visual Studio 2013\Projects\Vis\Vis\LocalDbVisTest.mdf"";Integrated Security=True";

Thank you for any help or hint in advance!

Upvotes: 1

Views: 4165

Answers (1)

dav_i
dav_i

Reputation: 28157

Instead of using an absolute path for your connection string, use

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

The reason your program cannot find the database is because it is looking in

C:\Users\idiot\Documents\Visual Studio 2013\Projects\Vis\Vis\LocalDbVisTest.mdf

Which, presumably, doesn't exist on your client's machine.

You can manually set your DataDirectory by using AppDomain.CurrentDomain.SetData("DataDirectory", path). You can get the path of your executable by using AppDomain.CurrentDomain.BaseDirectory

Upvotes: 5

Related Questions