如何监听与数据库的连接

时间:2014-09-16 03:56:08

标签: c# sql-server database

我正在编写 SQL Server 监听连接应用程序;

如果在启动或处理过程中无法连接(连接电缆断开/损坏),我的APP将一直睡眠,直到连接可用。恢复连接后,应用程序将唤醒并开始执行SQL命令。

请告诉您监听SQL连接所需的方法。

注意:我认为我需要理解一种类似于MessageQueue.Receive()方法机制的监听机制。

1 个答案:

答案 0 :(得分:0)

编写整个实现是一件很复杂的事情,我可以给你指点 您需要一个计时器来监控互联网连接是否可用。我已根据您的需要编写了部分逻辑。您可能需要通过保留私有变量来监视连接链接连接/重新连接。

public class Example
{
    /// <summary>
    /// External method for checking internet access:
    /// </summary>
    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

    /// <summary>
    /// C# callable method to check internet access
    /// </summary>
    public static bool IsConnectedToInternet()
    {
        int Description;
        return InternetGetConnectedState(out Description, 0);
    }

    private static Timer aTimer;

    /// <summary>
    /// Fired when a connection is made to the server
    /// </summary>
    public event EventHandler ConnectedToNetwork;

    public void OnConnected()
    {
        try
        {
            if (null != ConnectedToNetwork)
            {
                ConnectedToNetwork(this, new EventArgs());
            }
        }
        catch (Exception ex)
        {

        }
    }

    /// <summary>
    /// Fired when loses its connection to the server
    /// </summary>
    public event EventHandler DisconnectedFromNetwork;


    /// <summary>
    /// Clears server notifications
    /// </summary>
    public void OnDisconnected()
    {
        try
        {

            if (null != DisconnectedFromNetwork)
            {
                DisconnectedFromNetwork(this, new EventArgs());
            }
        }
        catch (Exception ex)
        {

        }
    }


    public void Intialize()
    {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(2000); //Timer will start when your application is started
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program... ");
        Console.ReadLine();
        Console.WriteLine("Terminating the application...");
    }
    private void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        if (IsConnectedToInternet())
        {
            OnConnected();
            //Code to APP will wake up and execute SQL commands.
        }
        else
        {
            OnDisconnected();
        }
    }
}

// DBClass 'ClientCodeSubscription' should verify whetehr the DB is up and running. 
class DBClass
{
    public bool DBServerIsRunning { get; set; }
    //In DB area
    private void ClientCodeSubscription()
    {
        if (DBServerIsRunning) //Check to see if DB service is running.
        {
            Example ex = new Example();
            ex.ConnectedToNetwork += Example_ConnectedToNetwork; //this is your client code probably in                this case the DB
        }

    }

    static void Example_ConnectedToNetwork(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}