Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 524331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:33:25+00:00 2026-05-13T08:33:25+00:00

I have a method to check and make sure my SQL server is online,

  • 0

I have a method to check and make sure my SQL server is online, which I use in some connection sensitive parts of my code.

Although it works fine, I notice it takes upto 20ms to run, and I was wondering if anyone knows of a better way of checking SQL server to ensure its up and kicking.

Here is my existing code.

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public static bool IsSqlServerOnline(string connectionString)
        {
#if DEBUG || DEBUG_SINGLE_CORE
            Stopwatch sw = new Stopwatch();
            sw.Start();
#endif

#if DEBUG || DEBUG_SINGLE_CORE
            // This sould only occur of we are in the VSTS designer
            if (string.IsNullOrEmpty(connectionString))
            {
                return false;
            }
#endif

            if ( !NetworkInterface.GetIsNetworkAvailable() )
            {
                return false;
            }

            if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException("connectionString");

            bool isSqlServerOnline = false;
            SqlConnectionStringBuilder conString = new SqlConnectionStringBuilder(connectionString);

            try
            {
                using (Ping p = new Ping())
                {
                    string sqlSvr = conString.DataSource;
                    if (sqlSvr != null)
                        isSqlServerOnline = p.Send(sqlSvr).Status == IPStatus.Success;
                }
            }
            catch (PingException)
            {
                isSqlServerOnline = false;
            }

            if ( isSqlServerOnline )
            {
                try 
                {
                    conString.ConnectTimeout = 3;
                    using (SqlConnection conn = new SqlConnection(conString.ToString()))
                    {
                        conn.Open();
                        isSqlServerOnline = true;
                    }
                }
                catch ( Exception )
                {
                    isSqlServerOnline = false;
                }
            }

#if DEBUG || DEBUG_SINGLE_CORE
            sw.Stop();
            Trace.WriteLine(string.Format("IsSqlServerOnline: {0}", sw.ElapsedMilliseconds));
#endif
            return isSqlServerOnline;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:33:26+00:00Added an answer on May 13, 2026 at 8:33 am

    As others have mentioned, the 20ms barrier is probably not anything to worry about. However, I might ask how often the code gets called? Would it be sufficient to cache the result of the call for 3-5 seconds (or even just 2-3)? instead of accessing the network each time? 2-3 seconds is a pretty small cache window, and there’s nothing to say that a “server alive” check couldn’t return OK only to have the server crash in the middle of the code executing anyway. If all you’re wasting is 20 ms every few seconds (at most), that’s hardly anything to worry about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 286k
  • Answers 286k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer NSString *xyz=[NSString stringWithFormat:@"%i %@",10,@"Sagar"]; This will create autoreleased instance of… May 13, 2026 at 4:54 pm
  • Editorial Team
    Editorial Team added an answer See Using jQuery to directly call ASP.NET AJAX page methods… May 13, 2026 at 4:54 pm
  • Editorial Team
    Editorial Team added an answer the following matches as you require ignoring accents: var Language… May 13, 2026 at 4:54 pm

Related Questions

I have a LINQ partial class: public partial class resp { public IEnumerable<RuleViolation> GetRuleViolations()
I am wondering how should I be grouping my repositories? Like from the examples
I have a standard many-to-many relationship between users and roles in my Rails app:
Let's say in my user model I have a ChangePassword method. Given an already
On my main window I have a DataGridView that I wish to display my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.