检查是否安装了SQL服务器(任何版本)?

时间:2022-08-15 07:01:53

I need to find if SQL server is installed on a machine. It could be any version of SQL server (7, 2005,8, sql express etc). We need to know this information as we are writing an installer and need to show to the user that if SQL server has not been found, the installation cannot proceed.

我需要查找是否在计算机上安装了SQL Server。它可以是任何版本的SQL服务器(7,2005,8,sql express等)。我们在编写安装程序时需要知道此信息,并且需要向用户显示如果未找到SQL服务器,则无法继续安装。

I have seen versions that use the registry, wmi, SMO or simply just connect to SQL server instance (although would not help here as we do not know the server name).

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

We are using the Wix Installer.

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

What is the correct way to do this?

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

JD

JD

4 个解决方案

#1


16  

A simple way to list all SQL Servers on the network is this:

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

Taken from this blog post.

取自这篇博客文章。

#2


2  

Have a look at this question: How can I determine installed SQL Server instances and their versions?

看看这个问题:如何确定已安装的SQL Server实例及其版本?

One of the answers lists the registry keys you could check to determine the installed SQL Server version(s).

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

Or check this codeproject article if you need to find any SQL Servers in the local network: http://www.codeproject.com/KB/database/locate_sql_servers.aspx

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

#3


2  

I needed something similar, to discover a local SQLServer instance to perform automated tests against.

我需要类似的东西,发现一个本地SQLServer实例来执行自动化测试。

The SmoApplication was perfect for this requirement - my code looks like this:

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;
}

#4


2  

Another simple alternative would be to use the following command line inside your installer:

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

sc queryex type= service | find "MSSQL"

The command above simply lists the all the services containing the MSSQL part, listing named and default SQL Server instances. This command returns nothing if nothing is found. It returns something like this:

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

SERVICE_NAME: MSSQL$SQLEXPRESS

Hope this helps.

希望这可以帮助。

#1


16  

A simple way to list all SQL Servers on the network is this:

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

Taken from this blog post.

取自这篇博客文章。

#2


2  

Have a look at this question: How can I determine installed SQL Server instances and their versions?

看看这个问题:如何确定已安装的SQL Server实例及其版本?

One of the answers lists the registry keys you could check to determine the installed SQL Server version(s).

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

Or check this codeproject article if you need to find any SQL Servers in the local network: http://www.codeproject.com/KB/database/locate_sql_servers.aspx

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

#3


2  

I needed something similar, to discover a local SQLServer instance to perform automated tests against.

我需要类似的东西,发现一个本地SQLServer实例来执行自动化测试。

The SmoApplication was perfect for this requirement - my code looks like this:

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;
}

#4


2  

Another simple alternative would be to use the following command line inside your installer:

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

sc queryex type= service | find "MSSQL"

The command above simply lists the all the services containing the MSSQL part, listing named and default SQL Server instances. This command returns nothing if nothing is found. It returns something like this:

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

SERVICE_NAME: MSSQL$SQLEXPRESS

Hope this helps.

希望这可以帮助。