我需要查找计算机上是否安装了SQL Server。它可能是任何版本的SQL Server(7、2005、8、SQL Express等)。我们需要知道这个信息,因为我们正在编写一个安装程序,并需要向用户显示,如果未找到SQL Server,则无法继续安装。
我见过一些使用注册表、WMI、SMO或仅连接到SQL Server实例的版本(尽管这里不适用,因为我们不知道服务器名称)。
我们正在使用WiX Installer。
哪种方法才是正确的呢?
JD
我需要查找计算机上是否安装了SQL Server。它可能是任何版本的SQL Server(7、2005、8、SQL Express等)。我们需要知道这个信息,因为我们正在编写一个安装程序,并需要向用户显示,如果未找到SQL Server,则无法继续安装。
我见过一些使用注册表、WMI、SMO或仅连接到SQL Server实例的版本(尽管这里不适用,因为我们不知道服务器名称)。
我们正在使用WiX Installer。
哪种方法才是正确的呢?
JD
列出网络上所有 SQL Server 的简单方法如下:
using System.Data;
using System.Data.Sql;
using System;
...
SqlDataSourceEnumerator sqldatasourceenumerator1 = SqlDataSourceEnumerator.Instance;
DataTable datatable1 = sqldatasourceenumerator1.GetDataSources();
foreach (DataRow row in datatable1.Rows)
{
Console.WriteLine("****************************************");
Console.WriteLine("Server Name:"+row["ServerName"]);
Console.WriteLine("Instance Name:"+row["InstanceName"]);
Console.WriteLine("Is Clustered:"+row["IsClustered"]);
Console.WriteLine("Version:"+row["Version"]);
Console.WriteLine("****************************************");
}
参考这篇博客文章。
另一个简单的替代方案是在您的安装程序中使用以下命令行:
sc queryex type= service | find "MSSQL"
SERVICE_NAME: MSSQL$SQLEXPRESS
我需要类似的东西,来发现本地的SQLServer实例,以便进行自动化测试。
SmoApplication非常适合这个需求——我的代码看起来像这样:
public static string GetNameOfFirstAvailableSQLServerInstance()
{
// Only search local instances - pass true to EnumAvailableSqlServers
DataTable dataTable = SmoApplication.EnumAvailableSqlServers(true);
DataRow firstRow = dataTable.Rows[0];
string instanceName = (string)firstRow["Name"];
return instanceName;
}
public static bool CheckSQLInstalled()
{
bool isOk1 = false;
bool isOk2 = false;
RegistryView registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
if (Environment.Is64BitOperatingSystem)
{
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
{
RegistryKey instanceKey = hklm.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
if (instanceKey != null)
{
foreach (var instanceName in instanceKey.GetValueNames())
{
isOk2 = true;
break;
}
}
}
}
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
{
RegistryKey instanceKey = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
if (instanceKey != null)
{
foreach (var instanceName in instanceKey.GetValueNames())
{
isOk1 = true;
break;
}
}
}
return isOk1 || isOk2;
}
public static bool CheckInstanceInstalled()
{
bool isOk1 = false;
bool isOk2 = false;
RegistryView registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
if (Environment.Is64BitOperatingSystem)
{
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
{
RegistryKey instanceKey = hklm.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
if (instanceKey != null)
{
foreach (string instanceName in instanceKey.GetValueNames())
{
if (instanceName.ToUpperInvariant() == "DATABASE_NAME")
{
isOk2 = true;
break;
}
}
}
}
}
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
{
RegistryKey instanceKey = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
if (instanceKey != null)
{
foreach (var instanceName in instanceKey.GetValueNames())
{
if (instanceName.ToUpperInvariant() == "DATABASE_NAME")
{
isOk1 = true;
break;
}
}
}
}
return isOk1 || isOk2;
}
添加对 System.ServiceProcess
的引用
然后我们可以使用 SQL 字符串查询所有服务并获取任何服务
ServiceController[] sc = ServiceController.GetServices();
foreach (ServiceController item in sc)
{
if (item.ServiceName.Contains("SQL"))
{
MessageBox.Show($@"Service Name: {item.ServiceName}"+"\n"+$@" Status: {item.Status}");
}
}