Don't Mark it as Duplicate or On Hold. I have tried all the solutions given in these answers here 1, 2 , 3 and 4. Following is my piece of code.
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='D:\Codes\Test\MyDataBase.mdf';Integrated Security=True;";
SqlConnection con = new SqlConnection(connectionString);
string query = "select * from StudentInfo";
SqlCommand cmd = new SqlCommand(query, con);
con.Open(); //Exception Appears Here...
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Console.WriteLine(dr[0] + " " + dr[1] + " " + dr[2] + " " + dr[3]);
}
}
con.Close();
Following Exception appears when I open the connection:
System.Data.SqlClient.SqlException: 'An attempt to attach an auto-named database for file MyDataBase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'
I added a Service-Based Database in my Project made in Visual Studio 2017 Version 15.6.4. I have tried many solutions from different websites too , Does its solution really exits? or we have to make a new project and start from scratch again?