-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.

moons
  • 209
  • 2
  • 12
  • 1
    What is the error message you are getting? – Md. Suman Kabir Dec 12 '17 at 11:49
  • 1
    Are you able to connect to it via sql management studio ? – Chetan Dec 12 '17 at 12:25
  • Yes, connecting via sql management studio is working perfectly fine. – moons Dec 12 '17 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. – CodeCaster Dec 12 '17 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 – moons Dec 12 '17 at 13:49

2 Answers2

-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.

rodrigogq
  • 1,943
  • 1
  • 16
  • 25
  • 1
    [`Server` and `Data Source` are the same thing](https://stackoverflow.com/questions/15025055/server-vs-data-source-in-connection-string). – CodeCaster Dec 12 '17 at 12:32
  • @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. – rodrigogq Dec 12 '17 at 13:40
-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"
Steef
  • 303
  • 2
  • 11
  • My variable contains one backslash: "nameofmyserver\nameofmyinstance" - is this a problem? – moons Dec 12 '17 at 12:47
  • 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. – Steef Dec 12 '17 at 12:54
  • 1
    You need to escape a backslash when specifying strings in source. You don't need a double backslash in the actual string. – CodeCaster Dec 12 '17 at 13:27