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.