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.
A named instance is identified by the network name of the computer plus the instance name that you specify during installation. The client must specify both the server name and the instance name when connecting. By default, SQL Server installs in the default instance unless you specify an instance name.
"Data Source=ServerName\InstanceName" specifies that the connection is being established to the named instance InstanceName on the server whose name is ServerName. The LocalDB Named Instance connection string is expressed as follows.
There is no way to change a default instance to a named instance. You must uninstall the default instance (unless you want to leave it for other purposes), and reinstall as a named instance.
The default instance name is MSSQLSERVER. For example, assume that an application program in Java or . NET sends data to a database in the default instance. It does not require the client to specify the name of the instance to make the connection.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With