Connection string for default instance like for named instance
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.
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
.
You should use Server=servername\instance_name
or Server=address\instance_name
精彩评论