I actually resolved this by using "Database=" instead of "Initial Catalog=". Thank you for your prompt response!!
Connection to local SQL Server Database Fail
I have a DB that I can access in MS SQL Server Management Studio and from an application that I used to create the DB using code first. I am trying to connect to that DB via a different app and my connection fails every time. In Visual Studio > SQL Server Object Explorer it lists the SQL Server as "(localdb)\MSSQLLocalDB". When I try to use a single "\" in my connection string VS prompts me saying unrecognized escape sequence and won't build. When I use "\" I can't connect. What am I missing?
string connStr = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=myDatabaseName;Integrated Security=True";
SqlConnection con = new SqlConnection(connStr);
try
{
con.Open();
MessageBox.Show("Connected");
}
catch
{
MessageBox.Show("Failed");
}
con.Close();
1 additional answer
Sort by: Most helpful
-
Viorel 112.1K Reputation points
2021-03-19T16:22:58.863+00:00 What error do you get?
. . . } catch( Exception exc ) { MessageBox.Show( exc.Message ); }