Use C # To determine whether the database server has been started

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.