如何检查mysql服务是否正在运行

问题描述:

你好...
我想确保我的应用程序首次尝试从mysql提取数据时的mysql服务...你能建议我任何事情吗...


在此先感谢...

Hello...
I wanna ensure for mysql service when my app is first time trying to fetch data from mysql...Can u suggest me anything...


Thanks in advance...

这里是了解服务是否正在运行的一种通用方法
Here is a general way to know whether service is running or not
public bool CheckIfServiceIsRunning(string serviceName)
{
    ServiceProcess.ServiceController mySC = new ServiceProcess.ServiceController(serviceName);
    if (mySC.Status == ServiceProcess.ServiceControllerStatus.Running) {
        // Service already running
        return true;
    }
    else {
        return false;
    }
}


我知道的唯一方法是在try..catch块中进行MySqlConnection.Open试用.如果引发异常,则您将无法连接(或至少不能使用该连接字符串).您可以使用MySqlConnection.Ping方法,但是由于此方法仍然需要打开连接...:笑:
The only way I know of, is to do a trial MySqlConnection.Open in a try..catch block. If an exception is raised, then you cannot connect (or at least, not with that connection string). You can use the MySqlConnection.Ping method, but since that needs an open connection anyway...:laugh: