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
,如行。
Initialization
方法只打开与数据库服务器的连接。Start
方法实际上使用command
注册一个依赖项。你的程序需要调用这两个函数。
OnDependencyChange
。
如果您查看SqlNotificationEventArgs
的参考页面,它不会告诉您哪些行已经更改—您必须自己查询数据库才能确定。