AndroidBegginer
AndroidBegginer

Reputation: 69

What is the better way to store mdf file with installation

I made a perfect application and I have to make insallation for that app. But i have a localDB and there is my problem, how, and where to store mdf file. I had searched about it on google but I haven't arrived at a solution yet.

I have tried two solutions.

  1. Include mdf file to installation folder via exe file. When I do that , program starts and executes fine, database works untill i have update or insert commands, exception is thrown which says that db is read-only. I tried running with administrator or changing privilegies, but still same problem.
  2. Save mdf file to mydocuments or appdata That way I create special folder via VS installer and add mdf file there, but problem is I don't know how to make relative connection string, because it is set to datadirectory.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <connectionStrings>

    <add name="Test1" connectionString=" Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Artikli.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

  </connectionStrings>

</configuration>

Where other programs store their databases? What is the best way to do it?

Upvotes: 1

Views: 347

Answers (1)

Tavares
Tavares

Reputation: 513

You should set up your connectionstring programmatically in some special folder in which the user will have access, like this:

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\your folder\your app\your database.mdf";
        string connectionString = string.Format("Data Source = {0}; Integrated Security = True; providerName = System.Data.SqlClient;", path);

..and when the program starts you should verify if this database is already saved in the specified folder, if not, you can copy the database from installation folder to there.

File.Copy(Application.StartupPath + @"\your database.mdf", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\your folder\your app\your database.mdf");

Upvotes: 1

Related Questions