Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
                }
            }
like image 914
ShaneKm Avatar asked Jan 24 '13 06:01

ShaneKm


People also ask

How can I set an SQL Server connection string?

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

WHAT IS instance name in connection string?

The Named Instance connection string is expressed as follows. Data Source= ServerName\InstanceName;Initial Catalog=DatabaseName; Authentication=Active Directory Integrated.

What should be the connection string for SQL Server?

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.


1 Answers

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))
{

}
like image 192
Yuriy Vikulov Avatar answered Oct 19 '22 06:10

Yuriy Vikulov