From Server \ Instance, how to determine service dependency?

I have a setup program in which a user enters a data source into a connection string (ie DataSource = "machine name \ instance").

Using C # named machinename \ instance string, I need to know if the service name is MSSQLSERVER or MSSQL $ SQLEXPRESS

SQL Server 2005 (full) and SQL Express 2008 are installed on one specific machine.

The reason I should know this is in our wix installer, the main application that will be installed has a dependency on the SQL server, so we need to have the right dependency to install it. Dependence can be on the MSSQLSERVER service or MSSQL $ SQLEXPRESS service, and I can install both of these services on the machine.

Jd

+4
source share
3 answers

SQL Server services are called either MSSQLSERVER (default instance) or MSSQL $ INSTANCENAME (named instances). You can determine whether it is a named instance or from a connection string (if it is in the form host\instance , a named instance, if is host , then it is the default instance), but, in truth, it is not, because:

  • connection string can use SQL client alias
  • the connection string can connect to a named instance listening on the default port
  • the connection string can connect to an explicit port and not specify an instance name

Thus, a more reliable way is to connect and query the instance name:

 SELECT SERVERPROPERTY('InstanceName'); 

If the return is NULL, the service name will be MSSQLSERVER, otherwise MSSQL $ ... You can even set this directly in the query:

 SELECT COALESCE('MSSQL$'+cast(SERVERPROPERTY('InstanceName') as sysname), 'MSSQLSERVER'); 
+4
source

You can use the ServiceController.GetServices method to get a list of all the services on the current computer. You can then use the ServiceName property for each of them to determine if each service you are looking for is installed. For instance:

 ServiceController[] services = ServiceController.GetServices(); if (services.Any(x => x.ServiceName == "MSSQL$SQLEXPRESS")) { this.serviceInstaller1.ServicesDependedOn = new string[] { "MSSQL$SQLEXPRESS" }; } else if (services.Any(x => x.ServiceName == "MSSQLSERVER")) { this.serviceInstaller1.ServicesDependedOn = new string[] { "MSSQLSERVER" }; } 
+2
source

Do you know significant harm depending on how?

You can get a list of running services using:

 using System.ServiceProcess; // ... ServiceController[] sc = ServiceController.GetServices(); 
0
source

Source: https://habr.com/ru/post/1303270/


All Articles