publicboolIsMySqlExist(string strSerVice){bool flag =false;//需引用system.serviceProcess
ServiceController[] services = ServiceController.GetServices();foreach(ServiceController service in services){if(service.DisplayName.ToString()== strSerVice){
flag =true;}}return flag;}
判断mysql服务是否在运行
publicboolisServiceRunning(string strSerVice){bool flag =false;//需引用system.serviceProcess
ServiceController[] services = ServiceController.GetServices();foreach(ServiceController service in services){if(service.DisplayName.ToString()== strSerVice){if(service.Status == ServiceControllerStatus.Running)
flag =true;}}return flag;}
启动服务
publicboolStartService(string strSerVice){bool bo =false;
ServiceController[] services = ServiceController.GetServices();foreach(ServiceController service in services){if(service.DisplayName.ToString()== strSerVice){if(service.Status != ServiceControllerStatus.Running){try{
service.Start();
bo =true;}catch(Exception){
MessageBox.Show("未能启动MySQL57服务,请尝试以管理员身份重新运行","软件提示");
bo =false;}}else
bo =true;}}return bo;}
停止服务
publicboolStopService(string strSerVice){bool bo =false;
ServiceController[] services = ServiceController.GetServices();foreach(ServiceController service in services){if(service.DisplayName.ToString()== strSerVice){if(service.Status != ServiceControllerStatus.Stopped){
service.Stop();
bo =true;}else
bo =true;}}return bo;}
publicvoidRemoteOn(string strUser ="root"){string strSql =string.Format("UPDATE mysql.user SET host = '%' WHERE user = '{0}'", strUser);if(ExecDataBySql(strSql)>0){ExecDataBySql("flush privileges");//刷新}}
关闭远程访问
publicvoidRemoteOff(string strUser ="root"){string strSql =string.Format("UPDATE mysql.user SET host = 'localhost' WHERE user = '{0}' AND host= '%'", strUser);if(ExecDataBySql(strSql)>0){ExecDataBySql("flush privileges");//刷新}}
创建用户
publicboolCreateUser(string strUserName ="",string strPw ="123456"){string strSql =string.Format("CREATE USER '{0}' IDENTIFIED BY '{1}'", strUserName, strPw);if(ExecDataBySql(strSql)>0)returntrue;elsereturnfalse;}