如何验证是否安装了 SQL Express (2014) 以及哪个实例

How to verify if SQL Express (2014) is installed AND which Instance

目标

我想确定用户是否安装了 SQL Server Express 2014。版本对我很重要。然后我想确保这个用户在他的 2014 服务器上有实例 "SQLEXPRESS"。

当前代码

如果安装了 SQLEXPRESS,我有一个函数 returns 一个布尔值,但不考虑版本 (2008/2010/2012/2014)

Private Function SQLExpressInstalled() As Boolean
    Try
        Using key As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Microsoft SQL Server\", False)
            If key Is Nothing Then Return False

            Dim strNames() As String
            strNames = key.GetSubKeyNames

            'If we cannot find a SQL server registry key, we don't have SQL Server Express installed
            If strNames.Length = 0 Then Return False
            If strNames.Contains("SQLEXPRESS") Then
                Return True
            End If
        End Using
    Catch ex As Exception
        'MsgBox(ex.Message)
    End Try

    Return False
End Function

有没有办法查明版本以及给定版本上安装了哪个实例?

我可以展示一个关于如何使用 SqlDataSourceEnumerator 执行此操作的小示例,但我不确定它是否适合您。我让你测试一下

using System.Data.Sql;

SqlDataSourceEnumerator sqe = SqlDataSourceEnumerator.Instance;
DataTable dt = sqe.GetDataSources();

// Here the DataTable has a column called Version, 
// but in my tests it is always null, so let's go with
// the SELECT @@version approach

foreach (DataRow row in dt.Rows)
{
    SqlConnectionStringBuilder scb = new SqlConnectionStringBuilder();
    scb.DataSource = row.Field<string>("ServerName");
    if(!row.IsNull("InstanceName"))
        scb.DataSource += "\" + row.Field<string>("InstanceName");


    // Another major problem is the Authetication rules for the 
    // current instance, I just assume that IntegratedSecurity works also for you
    // scb.UserID = "xxxx";
    // scb.Password = "xxxx";
    scb.IntegratedSecurity = true;
    scb.InitialCatalog = "master";
    using (SqlConnection cnn = new SqlConnection(scb.ConnectionString))
    using (SqlCommand cmd = new SqlCommand("SELECT @@Version", cnn))
    {
        Console.WriteLine("Version for: " + row.Field<string>("ServerName"));
        cnn.Open();
        string result = cmd.ExecuteScalar().ToString();

        // Now a bit of parsing will be required to isolate the information needed
        Console.WriteLine(result));
    }
}

我从 marc_s 的一篇帖子中找到了另一个解决方案 here

代码为:

Private Sub SQLInformation()
    ' open the 64-bit view of the registry, if you're using a 64-bit OS
    Dim baseKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)

    ' find the installed SQL Server instance names
    Dim key As RegistryKey = baseKey.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL")

    ' loop over those instances
    For Each sqlInstance As String In key.GetValueNames()
        Console.WriteLine("SQL Server instance: {0}", sqlInstance)

        ' find the SQL Server internal name for the instance
        Dim internalName As String = key.GetValue(sqlInstance).ToString()
        Console.WriteLine(vbTab & "Internal instance name: {0}", internalName)

        ' using that internal name - find the "Setup" node in the registry
        Dim instanceSetupNode As String = String.Format("SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup", internalName)

        Dim setupKey As RegistryKey = baseKey.OpenSubKey(instanceSetupNode, False)

        If setupKey IsNot Nothing Then
            ' in the "Setup" node, you have several interesting items, like
            ' * edition and version of that instance
            ' * base path for the instance itself, and for the data for that instance
            Dim edition As String = setupKey.GetValue("Edition").ToString()
            Dim pathToInstance As String = setupKey.GetValue("SQLBinRoot").ToString()
            Dim version As String = setupKey.GetValue("Version").ToString()

            Console.WriteLine(vbTab & "Edition         : {0}", edition)
            Console.WriteLine(vbTab & "Version         : {0}", version)
            Console.WriteLine(vbTab & "Path to instance: {0}", pathToInstance)
        End If
    Next
End Sub