Connection to local SQL Server Database Fail

Marty 101 Reputation points
2021-03-19T15:48:44.097+00:00

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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,676 questions
{count} votes

Accepted answer
  1. Marty 101 Reputation points
    2021-03-19T16:27:09.9+00:00

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

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. 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 );
    }
    
    0 comments No comments