且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

净SQL Server数据库监测 - 插入,更新,删除

更新时间:2023-02-08 17:00:55

要监视更改SQL表或记录在SQL 2005 +,你可以利用的SqlDependency类。

To monitor for changes to a SQL table or record in SQL 2005+ you can utilize the SqlDependency class.

这是针对ASP.NET中使用或中间层服务,而一台服务器是针对数据库管理活动的订阅 - 而不是多个几百个客户管理订阅。根据您指的是你的客户端的最大数量可能要建立一个可管理对SQL的通知订阅您的客户端缓存池服务。

It is targeted for use in ASP.NET or a middle-tier service where a single server is managing active subscriptions against a database - not multiple hundreds of clients managing subscriptions. Depending on the maximum number of clients you are referring to you may want to build a cache pooling service that can manage the notification subscriptions against SQL to your clients.

里面它采用SqlNotificationRequest它使用服务中介,不通知服务。因此,这应该前进的SQL 2008,等等。

Inside it uses SqlNotificationRequest which uses Service Broker, not Notification Services. Thus, this should work going forward on SQL 2008, etc.

MSDN上的SqlDependency

void Initialization()
{
    // Create a dependency connection.
    SqlDependency.Start(connectionString, queueName);
}

void SomeMethod()
{
  // Assume connection is an open SqlConnection.

  // Create a new SqlCommand object.
  SqlCommand command=new SqlCommand(
    "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", 
    connection);

  // Create a dependency and associate it with the SqlCommand.
  SqlDependency dependency=new SqlDependency(command);
  // Maintain the refence in a class member.

  // Subscribe to the SqlDependency event.
  dependency.OnChange+=new OnChangeEventHandler(OnDependencyChange);

  // Execute the command.
  command.ExecuteReader();
  // Process the DataReader.
}

// Handler method
void OnDependencyChange(object sender, 
   SqlNotificationsEventArgs e )
{
  // Handle the event (for example, invalidate this cache entry).
}

void Termination()
{
    // Release the dependency.
    SqlDependency.Stop(connectionString, queueName);
}

Using和监控SQL 2005查询通知的文章,讨论您完成步骤来设置它在你的code(使用一个Web应用程序为例)以及所需要的订阅服务代理适当的SQL权限等。

"Using and Monitoring SQL 2005 Query Notifications" article that talks you through the steps to set it up in your code (using a web app as example) along with the appropriate SQL permissions that are required to subscribe to Service Broker and so on.

这是 ASP.NET(网页缓存)类也是可用于Web方案。

An ASP.NET (web caching) class is also available for web scenarios.