When many projects are started, they need to connect to the database. Therefore, it is necessary to determine whether the database server is started. How can we determine whether the database server is started?
You can judge whether the database service is enabled. Of course, I have read the following information about the Registry. I will discuss the implementation method below:
/// <Summary>
/// Determine whether the database service has been started. If it has been started, True is returned; otherwise, False is returned.
/// </Summary>
/// <Returns> </returns>
Private bool JudgeDBServerStatus ()
{
Bool ExistFlag = false;
ServiceController [] service = ServiceController. GetServices ();
For (int I = 0; I <service. Length; I ++)
{
// When we install the database system server, we usually name it xxx, so that our database service name is MSSQL $ xxx by default,
// Of course, this is not exactly the case. It may vary according to the specific environment. You can also use the service display name to determine the service [I]. DisplayName.
If (service [I]. ServiceName. ToString (). Contains ("MSSQL $ "))
{
ExistFlag = true;
String strOuput = string. format ("Database Server started service name: {0}, service display name: {1} \ n", service [I]. serviceName, service [I]. displayName );
// Write the information to the log output file
DllComm. TP_WriteAppLogFileEx (DllComm. g_AppLogFileName, strOuput );
}
}
Return ExistFlag;
}
If any of the above methods is incorrect, we hope you can point it out. Thank you.
From weizhiai12's column