I'm attempting to save information to a local sqldatabase I've created to accept loss events for a process. However my issue at the moment is not an error message with regards to the code, but regarding the database itself I think. The error message box says, "An attempt to attach an auto-named database for file C: [Database file location] failed. A database with the same name exists, or specified file cannot be opened, or it is located in a UNC share."
I have looked else where but nothing directly relates to my issue/or has worked.
Pulling my hair out now! Thanks in advance.
My code is:
private void button1_Click(object sender, EventArgs e)
string constring = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=c: \\users\\user\\documents\\visual studio 2015\\Projects\\LossApplication\\LossApplication\\LossDB.mdf;Integrated Security=True; ";
string query=" insert into LossDB.LossTable (lossid,Equipment, Event, responsinility, start) values(@lossid, @equipment, @Cause, @reason, @start) ;";
SqlConnection conLossDB = new SqlConnection(constring);
SqlCommand cmdLossDB = new SqlCommand(query, conLossDB);
myReader = cmdLossDB.ExecuteReader();
MessageBox.Show("Loss Entry Saved");
In your connection String,Use Initial catalog instead of AttachDbFilename
string constring = "Data Source=(LocalDB)\MSSQLLocalDB; Initial Catalog = c: \users\user\documents\visual studio 2015\Projects\LossApplication\LossApplication\LossDB.mdf; Integrated Security=True; ";
Even i faced this issue,above code resolves.