views:

713

answers:

1

I'm using the following code to define a service dependency on SQL Server:

serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServicesDependedOn = new[] { "MSSQLSERVER" }; 
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);

This works in two machines, one with SQL Server and the other with SQL Server Express. But when I installed the service in the clients server, it failed because the SQL Server Express service name was different (SQLSERVEREXPRESS). Is there any way of defining a dependency that works in both situations? Thanks.

+1  A: 

You need to use the proper service name. The SQL service name is MSSQLSERVER for default instances, SQLSERVEREXPRESS for (some) Express instances and MSSQL$<instancename> for a named instance. Since the name is basically dynamic the best option is to enumerate the SQL Server services and pick the right name, or prompt the user if multiple choices are present.

Unfortunately I am not aware of any API to enumerate the installed SQL Server instances. Even MS support resorts to querying the registry:

Q. How do I determine how many instances of SQL Server are installed on a computer?

A: The names of all SQL Server instances on a computer can be found from the InstalledInstances value which is located under the following registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server

Remus Rusanu