簡體   English   中英

檢查是否安裝了 SQL 服務器(任何版本)?

[英]Check if SQL server (any version) is installed?

我需要查看 SQL 服務器是否安裝在機器上。 它可以是 SQL 服務器的任何版本(7、2005、8、sql express 等)。 我們在編寫安裝程序時需要知道這些信息,並且需要向用戶表明如果沒有找到 SQL 服務器,安裝將無法繼續。

我見過使用注冊表、wmi、SMO 或只是連接到 SQL 服務器實例的版本(盡管在這里沒有幫助,因為我們不知道服務器名稱)。

我們正在使用 Wix 安裝程序。

這樣做的正確方法是什么?

京東

列出網絡上所有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"

上面的命令只列出包含MSSQL部分的所有服務,列出命名和默認的SQL Server實例。 如果未找到任何內容,則此命令不返回任 它返回如下內容:

SERVICE_NAME: MSSQL$SQLEXPRESS

希望這可以幫助。

看看這個問題: 如何確定已安裝的SQL Server實例及其版本?

其中一個答案列出了您可以檢查以確定已安裝的SQL Server版本的注冊表項。

或者,如果需要在本地網絡中查找任何SQL Server,請查看此代碼項目文章: http//www.codeproject.com/KB/database/locate_sql_servers.aspx

我需要類似的東西,發現一個本地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;
}

另一個有用的但是,遲到(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;
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM