locked
Check and Refresh SQL Connection Status Every 5 Minutes RRS feed

  • Question

  • User1555919309 posted

    I am using MVC Core to develop application health dashboard. One feature I am developing is SQL Connection Check. 

    I want to:

    1. run check every 5 minutes.
    2. refresh only connection status check section. 

    On the dashboard, it displays Status: Up/Down. When the page open, I want it to check status every 5 minutes and refresh the connection status on the page. Is there a good way to do that? I have code for connection check. I just need to know how to implement check and display on the page every 5 minutes. Thanks.

            public bool CheckDbConnection()
            {
                IConfigurationRoot configuration = new ConfigurationBuilder()
                    .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                    .AddJsonFile("appsettings.json")
                    .Build();
    
                // using statement will close sql connection upon termination 
                using SqlConnection connection = new SqlConnection(configuration.GetConnectionString("DB"));
                try
                {
                    connection.Open();
                    return true;
                }
                catch (SqlException)
                {
                    return false;
                }
            }

    Pass the boolean value to Index View using viewmodel: 

    dashboard.isDbUp = CheckDbConnection();

    In the index.cshtml,    

            <div class="col-lg-3 col-xs-6">
                <!-- small box -->
                <div class="small-box @(@Model.isDbUp? "bg-green" : "bg-red")">
                    <div class="inner">
                        <h3>@(@Model.isDbUp? "Up" : "Down")<sup style="font-size: 20px"></sup></h3>
                        <p>SQL Status</p>
                        <p>SQL Connection Status</p>
    
                    </div>
                    <div class="icon">
                        <i class="fa fa-users"></i>
                    </div>
                    <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
                </div>
            </div>

    Thursday, March 4, 2021 3:14 PM

All replies

  • User475983607 posted

    I have code for connection check. I just need to know how to implement check and display on the page every 5 minutes.

    Use a JavaScript timer for a browser based application.  Windows application can uses the standard C# timer library

    If the action method is invoked outside and application then use Windows Task Scheduler.  

    Thursday, March 4, 2021 3:43 PM
  • User1555919309 posted

    Thank you so much for your answer mgebhard.

    Can I just refresh the connection status section on the page every 5 minutes?

    Thursday, March 4, 2021 3:58 PM
  • User475983607 posted

    TimJin

    Can I just refresh the connection status section on the page every 5 minutes?

    Use AJAX with a JavaScript timer for partial page updates.  Write a JavaScript timer that calls an AJAX function every 5 minutes.  Write JavaScript code in the AJAX response handler that updates the page.

    IMHO, the design does not make much sense.  The connection is status is only checked when someone has a browser open.  

    Thursday, March 4, 2021 4:11 PM
  • User1555919309 posted

    Yes. You are right. I am probably going to write a javascript and also make checkconnection function to httpget and call it from javascript. 

    Friday, March 5, 2021 1:47 AM