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

Posted

技术标签:

【中文标题】如何验证是不是安装了 SQL Express (2014) 以及哪个实例【英文标题】:How to verify if SQL Express (2014) is installed AND which Instance如何验证是否安装了 SQL Express (2014) 以及哪个实例 【发布时间】:2016-04-14 14:07:16 【问题描述】:

目标

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

当前代码

如果安装了 SQLEXPRESS,我有一个返回布尔值的函数,但不考虑版本 (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

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

【问题讨论】:

一旦你可以连接到它,然后使用 select @@version @Steve 我明白了。为了连接到正确的实例(假设我安装了 2008 和 2010),我必须拥有正确的实例。我怎么知道实例 SQLEXPRESS 不在 2010 年? 【参考方案1】:

我可以展示一个关于如何使用 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));
    

【讨论】:

好的,因为我在本地执行此操作,我想我可以将用户的计算机名称与使用您的代码找到的服务器进行比较?一旦计算机名称与本地服务器匹配,我就知道我在用户本地服务器上......对吗? 我想是的,但目前我只有两个 Sql Server 网络实例要测试所以......让我们等待是否有人有更好的方法来解决这个问题 目前这很有帮助,我会尝试用这种技术解决一些问题 从我注意到的是它无法获取“InstanceName”。它似乎总是为空。这是有原因的吗? 不知道,我没有要测试的 SQLEXPRESS 或命名实例【参考方案2】:

我从 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

【讨论】:

以上是关于如何验证是不是安装了 SQL Express (2014) 以及哪个实例的主要内容,如果未能解决你的问题,请参考以下文章

如何在SQL Server 2008 Express中更改sa密码?

sqlserver2012怎么验证是不是可用

如何检查用户是不是在 Firebase 和 Express/Node.js 中经过身份验证?

SQL Server Express 无法设置登录密码

SQL server 2008 express安装失败 数据库引擎 sql server复制安装失败

如何在客户端机器上安装 sql server express...?