ShaneKm
ShaneKm

Reputation: 21328

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

Upvotes: 6

Views: 24470

Answers (6)

Stefanos Zilellis
Stefanos Zilellis

Reputation: 611

Give a try with IP and port: connectionString = "Data Source=XXX.XXX.XXX.XXX,XXXX

Upvotes: 0

Arvo Bowen
Arvo Bowen

Reputation: 4929

I think this has to do with the way that SQL Server is setup on your system. I believe it is possible to install it in a way that the instance name is not needed (rather will not work if included).

So depending on how you install SQL Server you may need to provide an instance name or you may not need to.

If you are not sure or have no control over the installation method then I would try it with the instance name first (ex: Server=localhost\SQLServer2016Express) and if it fails as a fallback try to connect without the instance name (just connect to the host name only) ex: Server=localhost.

Upvotes: 0

user3355841
user3355841

Reputation: 1

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

use the @ sign to escape the \

Upvotes: -1

Jacco
Jacco

Reputation: 3271

Okay, you edited your question...

-- ignore ---

You are assigning connection strings in 2 locations:

  1. new SqlConnectionStringBuilder(GetConnectionString());
  2. builder.ConnectionString = "..."

What does GetConnectionString() do? What if you replace that with your connection string and leave out the second location?

-- end ignore --

Apart from that, here is a resource on the internet to look up the connection string you need: ConnectionStrings.com

Pick your database and find your string. If your string has the same format as they have there, there must be something wrong with your parameters.

Upvotes: 0

Yuriy Vikulov
Yuriy Vikulov

Reputation: 2499

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

}

Upvotes: 6

geordantan
geordantan

Reputation: 11

I think you forget to assigned the value to your connString.

SqlConnectionStringBuilder builder =
                    new SqlConnectionStringBuilder(GetConnectionString());

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

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

Upvotes: 0

Related Questions