简体   繁体   English

如何知道ac#程序中sql实例的状态

[英]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对象 - 具有StateStatus属性。

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.

相关问题 当c#程序崩溃怎么知道为什么? - when c# program crash how to know why? 如何在c#中关闭程序后保存复选框,文本框和...的状态 - How to save state of checkbox, textbox and … after closing a program in c# 如何在C#程序中访问LINQ中的Select实例的输出值 - how to access output values of Select instance in LINQ in a C# program 在C#程序中保存UI的状态 - Saving state of UI in a C# program 如何使用 Visual Studio 通过 C# 连接到 SQL 服务器实例 - How to connect to a SQL Server instance with C# using Visual Studio 如何有效地测试SQL Server实例是否正在C#中运行 - How to Efficently Test Whether an SQL Server Instance is Running in C# 我们如何通过c#获取本地SQL Server实例 - How we get local SQL Server instance through c# 如何从另一台计算机上的C#程序连接到SQL Server - How to connect to SQL Server from a C# program on another computer 如何将SQL连接字符串值传递给C#程序 - How to pass a sql connections string value to a C# program 如何在C#中知道程序在运行XP SP2的Mac上的Parallels上运行,还是在运行Windows 7的Mac上的VmWare上运行 - How to know in C# that program is running on Parallels on a Mac running XP SP2 or VmWare on a Mac running Windows 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM