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

本文关键字:SQL 事件 检测 应用程序 | 更新日期: 2023-09-27 18:20:34

我有下面的代码(需要澄清的是,这是MSDN网站上的文字复制/粘贴),它似乎可以正常连接(除了一个"拒绝访问"错误,因为我的安全请求还没有通过)。我需要做的是检测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);
            }
        }
    }
}

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

查看SQL Server通知。当某些基础数据集发生更改时,这将向您的应用程序发送信号。我不确定这对服务器来说有多重,所以如果你有大量客户端在等待通知,你应该仔细地进行负载测试。。。。