Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...
Posted

Here is a general way to know whether service is running or not
C#
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;
    }
}
 
Share this answer
 
Comments
OriginalGriff 22-Mar-11 3:23am    
Bear in mind that this will only work if the MySql instance is running on the local machine...
Prerak Patel 22-Mar-11 3:25am    
or you can use mySC.MachineName if you have sufficient rights.
Wayne Gaylard 22-Mar-11 3:27am    
Well, that is what the OP wants to check. This method will return true only if MySql Server is running, else it will return false, and so the user can deal with that.
Wayne Gaylard 22-Mar-11 3:25am    
Good answer. As the mysql service is called mysqld.exe, you can just call this method CheckIfServiceIsRunning(mysqld.exe).
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:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900