【问题标题】:Check if SQL server (any version) is installed?检查是否安装了 SQL Server(任何版本)?
【发布时间】:2010-03-04 16:49:02
【问题描述】:

我需要确定一台机器上是否安装了 SQL 服务器。它可以是任何版本的 SQL 服务器(7、2005、8、sql express 等)。我们在编写安装程序时需要知道这些信息,并且需要向用户表明如果没有找到 SQL Server,则安装无法继续。

我见过使用注册表、wmi、SMO 或只是连接到 SQL 服务器实例的版本(尽管在这里没有帮助,因为我们不知道服务器名称)。

我们正在使用 Wix 安装程序。

这样做的正确方法是什么?

京东

【问题讨论】:

  • 您的安装例程是否必须与 SQL 服务器在同一台服务器上运行?

标签: c# sql-server


【解决方案1】:

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

取自this blog post

【讨论】:

  • @Martin:谢谢,这正是我所需要的。我现在需要获取本地机器的机器名称,然后检查它是否在列表中。这样我就知道本机是否安装了sql实例。
  • @Martin 这不要求 SQL Browser 服务正在运行吗?
  • 我有一台机器,only SQL Server Analysis Services 服务器安装为独立服务。该框上没有no SQL 服务器引擎/实例。此代码是否会将 SQL Serve Analysis 服务服务器也仅视为 SQL Server 数据源?
【解决方案2】:

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

sc queryex type= service | find "MSSQL"

上面的命令只是列出了包含 MSSQL 部分的所有服务,列出了命名的和默认的 SQL Server 实例。如果未找到任何内容,则此命令不返回任何内容。它返回如下内容:

SERVICE_NAME: MSSQL$SQLEXPRESS

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    看看这个问题:How can I determine installed SQL Server instances and their versions?

    其中一个答案列出了您可以检查以确定已安装的 SQL Server 版本的注册表项。

    如果您需要在本地网络中查找任何 SQL Server,或者查看此 codeproject 文章:http://www.codeproject.com/KB/database/locate_sql_servers.aspx

    【讨论】:

    • 我们已经看到 sql server 的卸载留下了注册表项。此外,从之前的帖子来看,不能保证不同版本的注册表项相同。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      另一个有用但迟到(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;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多