6

In my .NET application I am connecting to Microsoft SQL Server 2005 or 2008 database. User selects instance which the application shows it and then application should do something with this instance. I take instance names from the registry, HKLM\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL.

I do not know if user selects default instance or named instance (and there is no such information in the Instance Names registry values). However, in order to connect to an arbitrary instance, I should use either

Server=(local)

or

Server=MSSQLSERVER\instance_name

in my ADO.NET connection string. Can I use only one connection string template? I tried to use Server=MSSQLSERVER\MSSQL10.MSSQLSERVER for my default SQL Server 2008 instance, but connection failed.

2 Answers 2

13

If the instance name is MSSQLSERVER then you must use a template like Server=servername. For all other instance names use a template like Server=servername\instancename. You cannot specify the default instance name explicitly.

As a side note you should not peak the registry for that. First of all, is undocumented and subject to arbitrary and unannounced changes. Second, is incorrect since instances that exist in the registry might not be running. And third is again incorrect because even running instances might not be connectable to if the SQL Browser service doesn't advertise them.

The recommended and supported way to detect existing instances is to use the SQL Browser broadcasting functionality, and they can be leveraged from the client code by using SmoApplication.EnumAvailableSqlServers.

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

3 Comments

Thank you for detailed answer. I have 2008 Server Express but my default instance is NOT MSSQLSERVER but MSSQL10.MSSQLSERVER. How do I?
The instance name is MSSQLSERVER. What you listed there is not an instance name, is an instance key, and I'm not going to explain what that is because, again, you should use the supported way instead (SMO).
Thanks for answering this years ago. Is this documented somewhere?
0

You should use Server=servername\instance_name or Server=address\instance_name

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.