views:

31

answers:

2

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

A: 

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

Mitch Wheat
This seems not working for default instance.
Alex
+1  A: 

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

As a sde note you should not peak the registry for that. First of all, is undocumented and sbject 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 SLQ 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.

Remus Rusanu
Thank you for detailed answer. I have 2008 Server Express but my default instance is NOT MSSQLSERVER but MSSQL10.MSSQLSERVER. How do I?
Alex
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).
Remus Rusanu
Thank you, Remus!
Alex