[英]How to know the state of an sql instance in a c# program
I have one database with one mirror in high-safety mode (using a witness server at the moment but planing to take him out), this database will be used to store data gathered by ac# program. 我有一个数据库,一个镜像处于高安全模式(目前使用见证服务器,但计划将他带出),该数据库将用于存储ac#program收集的数据。
I want to know how can I check in my program the state of all the SQL instances and to cause/force a manual failover. 我想知道如何在程序中检查所有SQL实例的状态并导致/强制手动故障转移。
is there any c# API to help me with this? 是否有任何c#API来帮助我这个?
info: im using sql server 2008 info:即时通讯使用sql server 2008
edit: I know I can query sys.database_mirroring but for this I need the principal database up and runing, I would like to contact each sql instance and check their status. 编辑:我知道我可以查询sys.database_mirroring但是为此我需要主数据库启动和运行,我想联系每个sql实例并检查它们的状态。
Use SQL Server Management Objects (SMO) . 使用SQL Server管理对象(SMO) 。
SQL Server Management Objects (SMO) is a collection of objects that are designed for programming all aspects of managing Microsoft SQL Server.
SQL Server管理对象(SMO)是一组对象,旨在编写管理Microsoft SQL Server的所有方面。 SQL Server Replication Management Objects (RMO) is a collection of objects that encapsulates SQL Server replication management.
SQL Server复制管理对象(RMO)是一组封装SQL Server复制管理的对象。
I have used SMO in managed applications before - works a treat. 我以前在托管应用程序中使用过SMO - 一种享受。
To find out the state of an instance, use the Server
object - is has a State
and a Status
properties. 要查找实例的状态,请使用
Server
对象 - 具有State
和Status
属性。
after playing around a bit I found this solution (i'm not if this is a proper solution, so leave comments plz) 在玩了一下之后我发现了这个解决方案(我不是,如果这是一个合适的解决方案,所以留下评论plz)
using Microsoft.SqlServer.Management.Smo.Wmi;
ManagedComputer mc = new ManagedComputer("localhost");
foreach (Service svc in mc.Services) {
if (svc.Name == "MSSQL$SQLEXPRESS"){
textSTW.Text = svc.ServiceState.ToString();
}
if (svc.Name == "MSSQL$TESTSERVER"){
textST1.Text = svc.ServiceState.ToString();
}
if (svc.Name == "MSSQL$TESTSERVER3") {
textST2.Text = svc.ServiceState.ToString();
}
}
this way i'm just looking for the state of the services (Running/Stoped) and is much faster, am I missing something? 这样我只是在寻找服务的状态(Running / Stoped)并且速度更快,我错过了什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.