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;
}
}
You can either use the new operator to make that directly. For example: SqlConnection conn = new SqlConnection( new SqlConnectionStringBuilder () { DataSource = "ServerName", InitialCatalog = "DatabaseName", UserID = "UserName", Password = "UserPassword" }. ConnectionString );
The Named Instance connection string is expressed as follows. Data Source= ServerName\InstanceName;Initial Catalog=DatabaseName; Authentication=Active Directory Integrated.
The following connection string will connect the database using windows authentication. Server=ServerName;Database=DatabaseName;Trusted_Connection=True; With help of the following C# code, this is how we can see the usage of a connection string in an application.
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))
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With