ShaneKm ShaneKm - 5 months ago 25
SQL Question

SQL Connection String with Instance Parameter

I'm trying to check (in .net c#) if I'm able to connect to SQL server. However whenever I specify instance in my connection string I'm no longer able to connect. :

This works:

builder.ConnectionString = "Server=DLS-534;user id=sa;password=Mypassword;initial catalog=master";


This DOES NOT work:

builder.ConnectionString = "Server=DLS-534\\SQL_2008_R2_DEV;user id=sa;password=Mypassword;initial catalog=master";


Why doesn't this work???. I do need to be able to connect to a specific instance because a user may have several dbs.

My full code:

SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder();

builder.ConnectionString = "Server=DLS-534\\SQL_2008_R2_DEV;user id=sa;password=Mypassword;initial catalog=master";

using (var connection = new SqlConnection(builder.ConnectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}

Answer

my working code with instance:

const string connStringWork = "Data Source=server\\instance;Initial Catalog=db;Integrated Security=True;Application Name=ЦС";

using (SqlConnection conn = new SqlConnection(connStringWork))
{

}