C# Sql依赖项不会触发
下面是一个例子 如何使用此代码在控制台中设置SqlDependency 我尝试将start方法放入C# Sql依赖项不会触发,c#,sql,sqldependency,C#,Sql,Sqldependency,下面是一个例子 如何使用此代码在控制台中设置SqlDependency 我尝试将start方法放入Initialization”方法中,但它没有触发SomeMethod` 另外,如果可能的话,我希望将一些参数传递给我的SqlNotificationEventArgs,例如行。初始化方法只打开到数据库服务器的连接。Start方法实际上使用命令注册依赖项。您的程序需要同时调用这两个函数 上述命令的查询结果更改时,将触发OnDependencyChange 如果您查看的参考页中有,它不会告诉您哪些行已
Initialization”方法中,但它没有触发
SomeMethod`
另外,如果可能的话,我希望将一些参数传递给我的
SqlNotificationEventArgs
,例如行。初始化方法只打开到数据库服务器的连接。Start
方法实际上使用命令注册依赖项。您的程序需要同时调用这两个函数
上述命令的查询结果更改时,将触发OnDependencyChange
如果您查看的参考页中有,它不会告诉您哪些行已更改——您必须自己查询数据库才能确定这一点
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);
}