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

  • SEARCH
  • Home
  • 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 6356097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:53:52+00:00 2026-05-24T22:53:52+00:00

I have a windows service which updates data to a database table in a

  • 0

I have a windows service which updates data to a database table in a remote machine. I wanted to check if the service works fine after the SQL server goes down and restarts for any reason (it may be SQL server software restart or the actual windows machine rebooting abruptly).

I have installed this windows service in 10 servers. Now, when I restarted the development sql server box, all the client service programs running in all the 10 boxes were unable to reconnect. However, it works fine after I restart the client windows service.

I want the client programs to automatically reconnect once the server is available and avoid restarting them manually.

Any advise please? Thanks in advance.
Below is the code I’m using to make a connection to the sql server.

private bool ConnectToSqlServer()
    {
        try
        {
            if (sqlConnection.State == ConnectionState.Closed)
            {
                sqlConnection.Open();
                return true;
            }
            return false;
        }
        catch(Exception Ex)
        {
            eventLog1.WriteEntry("ConnectToSqlServer " + Ex.Message, EventLogEntryType.Error);
            return false;
        }
    }

SQL Server – 2008 on Windows 2008 Enterprise R2.
Client program is C#, developed in visual studio 2010, which uses .net frame work 4.

  • 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-24T22:53:55+00:00Added an answer on May 24, 2026 at 10:53 pm

    I’m assuming you’re only trying to connect once and you’re getting an exception. If you’re not then the below will not work…

    Add a SqlException Catch block anywhere you access the database in your client. Then from that SqlException block invoke logic that retries to connect every x seconds for y iterations. Something like below:

    try
    { 
        //Database access code here
    }
    catch(SqlException sexc)
    {
        if (!ReconnectToDatabase())
        {
            // log error to event log
        }
    }
    
    private bool ReconnectToDatabase()
    {
       bool isConnected = false;
       for (int i = 0; i < 5; i++)  // 5 is arbitrary, use whatever retry count you want
       {
           if (ConnectToSqlServer())
           {
               break;
           }
           System.Threading.Thread.Sleep(5000);  // 5 seconds is arbitrary
       }
    }
    

    Again, the retry count and wait in seconds is arbitrary for this post. You will need to take into consideration the down time of your client as it waits, and how long it normally takes for SQL Server to restart.

    If you are trying the reconnect then in the SqlException block, call SqlConnection.ClearAllPools().

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

Sidebar

Related Questions

I have made a new windows service which works fine using barebone code (just
I have a windows service which fetches data from various datasources and build a
I have a windows service application which works using remoting. It is used to
I have a Windows Service which is basically accessing a mailbox and reading the
I have two windows application, one is a windows service which create EventWaitHandle and
I have a Windows Service application which uses a Threading.Timer and a TimerCallback to
I have implemented a small Windows Service which runs every 9 minutes and writes
I have a Windows Service written in Delphi which runs a number of programs.
I have been made a windows service.in which i have been added project installer.in
I have an importer process which is running as a windows service (debug mode

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.