Sql依赖不会触发

本文关键字:依赖 Sql | 更新日期: 2023-09-27 18:19:03

下面是来自https://msdn.microsoft.com/en-us/library/62xk7953%28v=vs.110%29.aspx

的示例
void Initialization()
{
    // Create a dependency connection.
    SqlDependency.Start(connectionString, queueName);
}
void SomeMethod()
{
    // Assume connection is an open SqlConnection.
    // Create a new SqlCommand object.
    using (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.
        using (SqlDataReader reader = command.ExecuteReader())
        {
            // Process the DataReader.
        }
    }
}
// Handler method
void OnDependencyChange(object sender, 
   SqlNotificationEventArgs e )
{
  // Handle the event (for example, invalidate this cache entry).
}
void Termination()
{
    // Release the dependency.
    SqlDependency.Stop(connectionString, queueName);
}

如何在控制台中使用这段代码来设置SqlDependency

我试着把开始方法Initialization' method, but it does not fire someemethod ' .

也-如果可能的话-我希望传递一些参数给我的SqlNotificationEventArgs,如行。

Sql依赖不会触发

Initialization方法只打开与数据库服务器的连接。Start方法实际上使用command注册一个依赖项。你的程序需要调用这两个函数。

当上述命令的查询结果发生变化时,将触发

OnDependencyChange

如果您查看SqlNotificationEventArgs的参考页面,它不会告诉您哪些行已经更改—您必须自己查询数据库才能确定。