C# 检查是否安装了 SQL 服务器(任何版本)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2381055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if SQL server (any version) is installed?
提问by JD.
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 服务器。它可以是任何版本的 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
京东
采纳答案by M4N
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.
取自这篇博文。
回答by M4N
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
回答by GarethOwen
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;
}
回答by Mário Meyrelles
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.
希望这可以帮助。
回答by Githithu Wambura
Another helpful but, late (10 years ago) answer:
另一个有用但已晚(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;
}