Taha Hussain
Taha Hussain

Reputation: 141

Can't access mdf file in desktop application

I need your help.. maybe it's the small task which I am not able to solve but I don't know how to search that on any search engine

I am using an .mdf file (vs 2103) in my WPF login register desktop application.. Everything is working fine, no compilation errors. I inserted some data into the .mdf file by using query in Visual Studio and then try to login through application and it's working fine. Moreover I am also able to register, i.e., insert into .mdf file from the register option of application if I use the "absolute path" of the .mdf file. Problem begins when I try to insert into .mdf file using a "relative path"... it doesn't insert data into the .mdf....I don't know why.

(this absolute path is not working. I have .mdf file in the database folder)

//connection string 
connetionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database\Database1.mdf;Integrated Security=True";
 
//code
SqlCommand sqlCmd;
string sql = null;
SqlConnection sqlCnn = new SqlConnection(connetionString);

try
{
    sqlCnn.Open();
    sql = "insert into [NewTable] ( Name , Password  )  values ('ABC' , '12345')";

    sqlCmd = new SqlCommand(sql, sqlCnn);
    sqlCmd.ExecuteNonQuery();
    sqlCnn.Close();
    MessageBox.Show("You have Been Registered");
}
catch (Exception ex)
{
    MessageBox.Show("" + ex);
}

It shows the message "You have Been Registered"

but is not visible in .mdf file... I want to ask..why is it behaving like this....and I want to use the relative path as we cant judge the client system absolute path...what mistake I am doing? how to handle this (i guess self made) issue?

Upvotes: 2

Views: 1005

Answers (1)

marc_s
marc_s

Reputation: 755471

The whole AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. MyDatabase)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=MyDatabase;Integrated Security=True
    

    and everything else is exactly the same as before...

Also see Aaron Bertrand excellent blog article Bad habits to kick: Using AttachDbFileName for more in depth explanations and tips

Upvotes: 1

Related Questions