5

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;
                }
            }
1
  • 5
    And what is the exact error message you get? Commented Jan 24, 2013 at 6:45

6 Answers 6

5
+50

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

}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

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.

Comments

0

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

Comments

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

use the @ sign to escape the \

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.