简体   繁体   English

检测SQL Server 2008 R2

[英]Detect SQL Server 2008 R2

I've googled a day but the question is still question: how to detect SQL Server 2008 R2 on various Windows versions: 我已经搜寻了一天,但问题仍然是:如何在各种Windows版本上检测SQL Server 2008 R2:

  • via registry (would be our favorite solution), 通过注册表 (将是我们最喜欢的解决方案),
  • via file system , 通过文件系统
  • via installer exit code ? 通过安装程序退出代码

Installer developed with NSIS. 使用NSIS开发的安装程序。 Some additional informations: 一些其他信息:

Registry 登记处

Samples on the net are out of date or simply improper. 网上的样本已过期或完全不正确。 Not just R2 but 2k8 detection is problematic too. 不仅R2,而且2k8检测也有问题。

File system 文件系统

I have no idea what files are especially from 2k8 R2. 我不知道什么文件特别来自2k8 R2。

Installer exit code 安装程序退出代码

In some cases exits without error code (ie prerequisites missing). 在某些情况下,退出时没有错误代码(即缺少先决条件)。

try to execute this query: 尝试执行以下查询:

SELECT @@Version

I get this result back: 我得到这个结果:

Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) Apr 2 2010 15:48:46 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 5.2 (Build 3790: Service Pack 2) Microsoft SQL Server 2008 R2(RTM)-10.50.1600.1(X64)2010年4月2日15:48:46版权所有(c)Windows NT 5.2(Build 3790:Service Pack 2)上的Microsoft Corporation Enterprise Edition(64位)

is this enough? 这够了吗? For me yes :) 对我来说是:)

you could use the WMI to listy all the microsoft product installed and then you could look foe the one you need 您可以使用WMI列出所有已安装的Microsoft产品,然后查找您需要的产品

public static class MyClass
    {
        public static void Main()
        {
            ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
            foreach (ManagementObject mo in mos.Get())
            {
                Console.WriteLine(mo["Name"]);
            }


        }
    }

查看具有多种方法的旧版SQLPing源代码

And before you try execute query provided by Davide (SELECT @@Version) you can check, that MSSQL service is running 在尝试执行Davide提供的查询(SELECT @@ Version)之前,您可以检查MSSQL服务是否正在运行

using System.ServiceProcess;
var list = ServiceController.GetServices().ToList();
        if (list.Any(sc => sc.ServiceName.ToLower().Contains("mssql")))

I use below code in my application 我在应用程序中使用以下代码

 SqlDataSourceEnumerator sqldatasourceenumerator1 = SqlDataSourceEnumerator.Instance;
            DataTable datatable1 = sqldatasourceenumerator1.GetDataSources();
            foreach (DataRow row in datatable1.Rows)
            {
                if (Environment.MachineName.Equals(row["ServerName"]))
                {

                    isSqlServerPresent = true;
                    break;
                }
            }

The only issue is, this code works on when the machine is on network, but since in my case machine will be on network, so I was ok with this issue. 唯一的问题是,该代码在计算机处于网络中时有效,但是由于我的情况是计算机将在网络中,所以我对这个问题很满意。

It gets me the local instance of SQL Server. 它为我提供了SQL Server的本地实例。

In fact if you want to list Servers in the network and SQL Servers instances on a machine or on the LAN, there are APIs for it. 实际上,如果要列出计算机或LAN上的网络中的服务器和SQL Server实例的列表,则有相应的API。

There should be proper way to call NetServerEnum Windows API, for examples see: 应该有正确的方法来调用NetServerEnum Windows API,有关示例,请参见:

http://www.xtremevbtalk.com/showthread.php?t=107256 http://www.xtremevbtalk.com/showthread.php?t=107256

http://pinvoke.net/default.aspx/netapi32/netserverenum.html http://pinvoke.net/default.aspx/netapi32/netserverenum.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM