C# SqlDependency不触发c
您好,我正在尝试在我的windows服务代码中实现Sql依赖关系。我已经将我的ServiceBroker设置为true。我已经在Windows On Start方法中编写了这段代码。当表中有新条目时,OnDependencyChange方法应立即启动。但它从未发生过。 这是我的密码C# SqlDependency不触发c,c#,asp.net-mvc,c#-4.0,C#,Asp.net Mvc,C# 4.0,您好,我正在尝试在我的windows服务代码中实现Sql依赖关系。我已经将我的ServiceBroker设置为true。我已经在Windows On Start方法中编写了这段代码。当表中有新条目时,OnDependencyChange方法应立即启动。但它从未发生过。 这是我的密码 System.Data.SqlClient.SqlDependency.Start("connection string;database=dbName;Integrated Sec
System.Data.SqlClient.SqlDependency.Start("connection
string;database=dbName;Integrated Security=True;");
SqlConnection connection = new SqlConnection("connection
string;database=dbName;Integrated Security=True;");
connection.Open();
using (SqlCommand command = new SqlCommand("SELECT * FROM
UserDetails", connection))
{
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new
OnChangeEventHandler(OnDependencyChange);
using (SqlDataReader reader = command.ExecuteReader())
{
// Process the DataReader.
}
}
void OnDependencyChange(object sender, SqlNotificationEventArgs e)
{
// Handle the event (for example, invalidate this cache entry).
}
请帮忙。我想你有安全问题。如果我没记错的话,使用SqlDependency所需的权限存在一个已知问题。 首先,您需要验证并最终启用ServiceBroker功能。
然后,您需要启用。只有在这之后,您才能启用SqlDependency事件来检测数据修改:您不能使用SELECT*FROM…,使用SELECT col1、col2、col3 FROM。。。谢谢你的回复,马丁。我也检查过了。不走运..除了显式指定您关心的列之外,您可能还需要指定表所在的架构-从dbo.UserDetails中选择col1、col2。如果仍然不起作用,请确保在您尝试连接的SQL Server实例上启用了Service Broker功能。