使用C#应用程序检测SQL事件

时间:2014-09-04 12:18:13

标签: c# sql events tcp listener

我有下面的代码(来自MSDN网站的文字复制/粘贴只是为了清楚),它似乎应该连接(除了#34;访问被拒绝"错误,这是好的因为我的安全请求尚未通过)。我需要做的是检测我们的sql server何时执行insertupdate操作。基本上,这个应用程序应该全天候运行并在这样的操作遇到监听器时执行某些功能。我不是要求在我面前摆放代码,但我想问从哪里开始。这是我不知道如何做到这一点,我被告知我有大约一个星期来弄明白并完成它。有人能指出我正确的方向吗?提前谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Connect_Server
{
    class Program
    {
        static string output = "";

        static void Main(string[] args)
        {
            createListener();
        }

        static public void createListener()
        {
            // Create an instance of the TcpListener class.
            TcpListener tcpListener = null;
            IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
            try
            {
                // Set the listener on the local IP address 
                // and specify the port.
                tcpListener = new TcpListener(ipAddress, 80);
                tcpListener.Start();
                output = "Waiting for a connection...";
            }
            catch (Exception e)
            {
                output = "Error: " + e.ToString();
                Console.Write(output);
            }
            while (true)
            {
                // Always use a Sleep call in a while(true) loop 
                // to avoid locking up your CPU.
                Thread.Sleep(10);
                // Create a TCP socket. 
                // If you ran this server on the desktop, you could use 
                // Socket socket = tcpListener.AcceptSocket() 
                // for greater flexibility.
                TcpClient tcpClient = tcpListener.AcceptTcpClient();
                // Read the data stream from the client. 
                byte[] bytes = new byte[256];
                NetworkStream stream = tcpClient.GetStream();
                stream.Read(bytes, 0, bytes.Length);
                SocketHelper helper = new SocketHelper();
                helper.processMsg(tcpClient, stream, bytes);
            }
        }

    }
}

1 个答案:

答案 0 :(得分:2)

结帐SQL Server Notifications。当某些基础数据集发生变化时,这将向您的应用发送信号。我不确定这对服务器来说有多重,所以如果你有大量的客户等待通知,你应该仔细加载测试....