Access MDF database from the executable folder

3

I'm having a annoying problem solving because I want my application to access the database file from the folder where the executable is, because on each computer installed this folder may be on a different drive.

I found that I should use the following code to solve the problem, where |DataDirectory| is supposed to refer to a folder in the project location, named App_Data , I created the folder, put my database there and I used the following code as a connection string:

con.ConnectionString = @"Data Source=.\SQLEXPRESS;
                          AttachDbFilename=|DataDirectory|\he_dados.mdf;
                          Integrated Security=True;
                          Connect Timeout=30;
                          User Instance=True";

But this simply does not work, and the system throws me the following exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Additional information: Falha da instância.

Honestly, I'm getting tired, everything I'm going to do in C # always has a silly hitch in the front.

    
asked by anonymous 15.12.2015 / 21:17

1 answer

4

Registering the solution in response:

con.ConnectionString = @"Data Source=(LocalDB)\v11.0;
                          AttachDbFilename=|DataDirectory|\he_dados.mdf;
                          Integrated Security=True;
                          Connect Timeout=30";
    
16.12.2015 / 00:29