user4618497
user4618497

Reputation: 73

Release a WPF app with local database

OK, so I am quite lost. This is my first real application written for windows with a local database. I have written an WPF application in VS2012 with a local database, which has all CRUD written as stored procedures. The app is working as I run it in VS. But as I build the app and install it (on any machine) the database is not accessible. I have a feeling the connection string is causing this issue.

First I used this connection string:

SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename='D:\LH\Personalitytest win\DB.mdf';Integrated Security=True;Database=DB")

But as I got "Error: 26 – Error Locating Server/Instance Specified" I guessed that the Data Source of the connection is not correct (although the AttachDbFilename is definitly incorrect). So, I changed the connection string to:

SqlConnection conn = new SqlConnection(@"Server=(localdb)\v11.0;AttachDbFilename=" + AppDomain.CurrentDomain.BaseDirectory + "DB.mdf;Database=DB;Trusted_Connection=Yes;");

And I got an error which basically said that the file DB.mdf is not found.

So, I made sure that the .mdf and .ldf files are copied to the installation directory/database. But after installation both these files are read only (even if these have read/write persmission as I am developing). I have tried to change the file permissions (for both files) as the application starts for the first time by:

string path = AppDomain.CurrentDomain.BaseDirectory + @"Database\DB.mdf";
FileInfo file = new FileInfo(path);
file.IsReadOnly = false;

and

File.SetAttributes(path, FileAttributes.Normal);

and

FileAttributes attributes = File.GetAttributes(path);
attributes = attributes & ~FileAttributes.ReadOnly;
File.SetAttributes(path, attributes);

but neither of these methods are working.

My question is basically, how should the database be implemented in the final release?

Upvotes: 2

Views: 678

Answers (1)

Halid
Halid

Reputation: 448

  • Your database should be located in your windows user directory

  • Other way is to install SqlServer Express on your machine. And configure your connection string accordingly.

Upvotes: 1

Related Questions