-1

I can't connect via ASP.NET to a named instance of a sql server, that itsn't the default instance. My servername is NAMEOFSERVER\NAMEOFINSTANCE.

If I try to connect to the default instance of the server, it works.

I use the following connection string:

connection_string = "Data Source=" + servername + ";Initial Catalog=" + db_catalog + ";User Id=" + user + ";Password=" + password + ";persist security info=False;Trusted_Connection=No;Connection Timeout=1000"

The SQL Server Browser service is running.

5
  • 1
    What is the error message you are getting? Commented Dec 12, 2017 at 11:49
  • 1
    Are you able to connect to it via sql management studio ? Commented Dec 12, 2017 at 12:25
  • Yes, connecting via sql management studio is working perfectly fine. Commented Dec 12, 2017 at 12:33
  • This is not a connection string, this is string concatenation. Debug your code and inspect that connection_string contains what you expect it to. You may have a \n escape sequence in there, for example. Commented Dec 12, 2017 at 12:34
  • I logged connection_string, and it contains what I expected it to. The strange thing is: my other named instance on the same server has got the same connection string (only the name of the instance inside the servername differs) and there the connection works Commented Dec 12, 2017 at 13:49

2 Answers 2

-1

Make sure your connection string have the pattern above:

var connectionString = $"Data Source=myServerName\\myInstanceName;Database=myDataBase;User Id=myUsername; Password=myPassword;",

Also, make sure you can connect via SQL Management Studio, since instances of the SQL Server can limit access to some users (make sure you connect to the same user name + password you want your app to use).


EDIT

If the user you are using to connect is not part of a Windows account, please open your instances security configuration and make sure Mixed Mode is enabled.

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

2 Comments

@CodeCaster Thanks, I updated my answer. My point was to make sure that the pattern was correct, including the slashes. I also updated with some other tips.
-3

You might want to check if your variable "servername" contains a double backslash. This is needed because backslash is a escape character.

connection_string = "Data Source=" + servername.Replace("\\", "\\\\") + ";Initial Catalog=" + db_catalog + ";User Id=" + user + ";Password=" + password + ";persist security info=False;Trusted_Connection=No;Connection Timeout=1000"

3 Comments

My variable contains one backslash: "nameofmyserver\nameofmyinstance" - is this a problem?
Did you try the ConnectionString I added? There the backslash is converted to a double backslash. My suggestion is that it will work when you try it like that.
You need to escape a backslash when specifying strings in source. You don't need a double backslash in the actual string.

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.