C# 检测 SQL 服务器是否正在运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22979/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:11:16  来源:igfitidea点击:

Detecting if SQL server is running

提问by steve_mtl

I'm looking for a way to poll different servers and check that SQL server is up and running. I'm writing my code in C#. I don't particularly care about individual databases, just that SQL server is running and responsive.

我正在寻找一种方法来轮询不同的服务器并检查 SQL 服务器是否已启动并正在运行。我正在用 C# 编写我的代码。我并不特别关心单个数据库,只关心 SQL 服务器正在运行和响应。

Any ideas?

有任何想法吗?

采纳答案by TheSmurf

Well, the brute force solution is to attempt to initiate a connection with the database on each server. That will tell you whether it's running, though you could have timeout issues.

好吧,蛮力解决方案是尝试启动与每个服务器上的数据库的连接。这将告诉您它是否正在运行,尽管您可能会遇到超时问题。

The more elegant (but more difficult... isn't that always the way?) solution would be to use WMI to connect to the remote machine and find out if the SQL server process is running.

更优雅(但更困难...不是总是这样吗?)解决方案是使用 WMI 连接到远程机器并找出 SQL 服务器进程是否正在运行。

回答by Vincent

Use the TCPClient Classto create a generic function that connects in TCP to a given IP address.

使用TCPClient 类创建一个通用函数,该函数在 TCP 中连接到给定的 IP 地址。

Then iterate over the list of servers you want to test and try to open a connection to port 1433.

然后遍历要测试的服务器列表并尝试打开到端口 1433 的连接。

回答by Stu

If you need specific servers, use WMI. If you just want all available servers:

如果您需要特定的服务器,请使用 WMI。如果您只想要所有可用的服务器:

http://support.microsoft.com/kb/q287737/

http://support.microsoft.com/kb/q287737/

回答by Shaun Austin

I would certainly go with Vincent's answer. Just make absolutely certain you are closing and disposing the tcp connections properly etc. WMI seems a bit of overkill to me if that is all you're after.

我当然会同意文森特的回答。只要绝对确定您正在关闭和正确处理 tcp 连接等。如果这就是您所追求的,WMI 对我来说似乎有点矫枉过正。

回答by Benjamin Autin

System.Data.Sql.SqlDataSourceEnumeratorwill return all instances of SQL Server currently running.
MSDN Link

System.Data.Sql.SqlDataSourceEnumerator将返回当前运行的所有 SQL Server 实例。
MSDN链接

回答by Vladimir

SqlDataSourceEnumerator gives you all instances but they are not necessarily running. For local instances of SQL, you can use ServiceController object, namespace System.ServiceProcess. Service name is concatination of "MSSQL$" and "InstanceName" from SqlDataSourceEnumerator. Set ServiceName property of the ServiceController object, and you can check "Status" property - Stopped, Running, Pended etc. Hence, you can filter "Running" ones

SqlDataSourceEnumerator 为您提供所有实例,但它们不一定正在运行。对于 SQL 的本地实例,您可以使用 ServiceController 对象,命名空间 System.ServiceProcess。服务名称是来自 SqlDataSourceEnumerator 的“MSSQL$”和“InstanceName”的串联。设置 ServiceController 对象的 ServiceName 属性,您可以检查“状态”属性 - 已停止、正在运行、已挂起等。因此,您可以过滤“正在运行”的