question

Marty-9161 avatar image
0 Votes"
Marty-9161 asked ·

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();

sql-server-generaldotnet-sqlclient
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Marty-9161, I am glad to know that your problem has been solved. In order to close this thread, please mark useful replies as answers. By doing so, it will benefit all community members who are having this similar issue. Your contribution is highly appreciated.

0 Votes 0 ·
Marty-9161 avatar image
0 Votes"
Marty-9161 answered ·

I actually resolved this by using "Database=" instead of "Initial Catalog=". Thank you for your prompt response!!

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Convert that comment to an Answer (under the More link) and Accept your post as Answer so that people browsing the forum sees that this question has been sorted out.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

What error do you get?

 . . .
 }
 catch( Exception exc )
 {
    MessageBox.Show( exc.Message );
 }
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.