检查是否安装了 SQL Server(任何版本)?

22

我需要查找计算机上是否安装了SQL Server。它可能是任何版本的SQL Server(7、2005、8、SQL Express等)。我们需要知道这个信息,因为我们正在编写一个安装程序,并需要向用户显示,如果未找到SQL Server,则无法继续安装。

我见过一些使用注册表、WMI、SMO或仅连接到SQL Server实例的版本(尽管这里不适用,因为我们不知道服务器名称)。

我们正在使用WiX Installer。

哪种方法才是正确的呢?

JD


你的安装程序必须在与 SQL 服务器相同的服务器上运行吗? - BIDeveloper
6个回答

17

列出网络上所有 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("****************************************");
}

参考这篇博客文章


@Martin:谢谢,这正是我需要的。现在我只需要获取本地计算机的机器名称,并检查它是否在列表中。这样我就知道本地计算机是否安装了 SQL 实例。 - JD.
5
需要运行SQL Browser服务才能实现这个吗? - Thomas
我有一台机器,上面只安装了SQL Server Analysis Services服务器作为独立服务。该盒子上没有SQL Server引擎/实例。这段代码是否也会将SQL Server Analysis服务服务器视为仅限于SQL Server数据源? - RBT

7

另一个简单的替代方案是在您的安装程序中使用以下命令行:

sc queryex type= service | find "MSSQL"

上述命令简单地列出了包含 MSSQL 部分的所有服务,列出了命名和默认 SQL Server 实例。如果未找到任何内容,则此命令不返回任何内容。它返回类似于以下内容:
SERVICE_NAME: MSSQL$SQLEXPRESS

希望这能帮到您。

2

我需要类似的东西,来发现本地的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;
}

2

我们曾经看到过卸载Sql Server时留下了注册表条目的情况。此外,在先前的帖子中,不能保证不同版本的注册表条目是相同的。 - JD.

1
另一个有帮助但晚了(10年前)的答案:

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;
    }

0

添加对 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}");
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接