如何知道c#程序中sql实例的状态 [英] How to know the state of an sql instance in a c# program

查看:20
本文介绍了如何知道c#程序中sql实例的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,一个镜像处于高安全模式(目前使用见证服务器,但打算将他取出),该数据库将用于存储由c#程序收集的数据.

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 a c# program.

我想知道如何在我的程序中检查所有 SQL 实例的状态并导致/强制进行手动故障转移.

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.

是否有任何 C# API 可以帮助我解决这个问题?

is there any c# API to help me with this?

信息:我使用的是 sql server 2008

info: im using sql server 2008

我知道我可以查询 sys.database_mirroring 但为此我需要启动并运行主体数据库,我想联系每个 sql 实例并检查它们的状态.

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.

推荐答案

在玩了一会儿之后我找到了这个解决方案(我不认为这是一个合适的解决方案,所以请留下评论)

after playing around a bit I found this solution (i'm not if this is a proper solution, so leave comments 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?

这篇关于如何知道c#程序中sql实例的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆