简体   繁体   中英

C# SQL server connection

HI As a daily check I have to check the SQL connection to all our servers. At the moment this is done by manually logging on through SQL server managment studio. I was just wondering if this can be coded in C# so that I can run it first thing in a morning and its checks each server and the instance for a valid SQL connection and reports back to say if the connection is live or not.

Thanks Andy

Here's a little console app example that will cycle through a list of connections and attempt to connect to each, reporting success or failure. Ideally you'd perhaps want to extend this to read in a list of connection strings from a file, but this should hopefully get you started.

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

namespace SQLServerChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use a dictionary to associate a friendly name with each connection string.
            IDictionary<string, string> connectionStrings = new Dictionary<string, string>();

            connectionStrings.Add("Sales Database", "< connection string >");
            connectionStrings.Add("QA Database", "< connection string >");

            foreach (string databaseName in connectionStrings.Keys)
            {
                try
                {
                    string connectionString = connectionStrings[databaseName];

                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        connection.Open();
                        Console.WriteLine("Connected to {0}", databaseName);
                    }
                }
                catch (Exception ex)
                {
                    // Handle the connection failure.
                    Console.WriteLine("FAILED to connect to {0} - {1}", databaseName, ex.Message);
                }
            }

            // Wait for a keypress to stop the console closing.
            Console.WriteLine("Press any key to finish.");
            Console.ReadKey();
        }
    }
}

Look at the SqlConnection Class. It includes a basic sample. Just put a loop around that sample to connect to each server, and if any server fails to connect it throws an exception.

Then just set it up as a scheduled task in Windows.

A nice way to report the status might be with an email, which can easily be sent out with SmtpClient.Send (the link has a nice simple sample.

Why c#? You could make a simple batch file that does this using the osql command.

osql -S servername\dbname -E -Q "select 'itworks'"

You can also have a look at this method:

SqlDataSourceEnumerator.Instance.GetDataSources()

It gives you a list of SQL servers available on the network.

You know, they make monitoring tools that let you know if your sql server goes down . . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM