【发布时间】:2018-09-22 19:22:29
【问题描述】:
我正在尝试使用 sql Dependency 来检查表数据是否已被其他客户端更改,我在这里遵循了教程,但是当我尝试创建队列时,我收到以下错误消息,这是我的 c# 代码和 sql 语句。
编辑 1 抱歉,我忘了在我的第一篇文章中包含教程,这里是教程中的链接,它声明了 quename,但没有说明如何创建它。
创建队列 ExportedOrdersChangeMessage 创建服务 ExportedOrdersService ON QUEUE ExportedOrdersChangeMessage ([http://schemas.microsoft.com/SQL/Notification/PostQueryNotification])
运行上述内容后我收到的错误如下。
消息 15151,第 16 级,状态 1,第 5 行 c'http://schemas.microsoft.com/SQL/Notification/PostQueryNotification', 因为它不存在或者你没有权限。
基本上我想要做的是,如果用户试图更改记录,我想向其他客户发送通知。这是这种方法的最佳方法吗?它是一个 winforms 业务应用程序,如果任何其他使这更容易的库会很棒。
我正在使用 sql Server 2017 express,我认为这会作为标准配置?
private void DetectChanges()
{
// Assume connection is an open SqlConnection.
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
using (SqlConnection con = new SqlConnection(connectionString))
{
// Create a new SqlCommand object.
using (SqlCommand command = new SqlCommand("SELECT exportedtoMeteor FROM dbo.exported", con))
{
// Create a dependency and associate it with the SqlCommand.
SqlDependency dependency = new SqlDependency(command);
// Maintain the reference in a class member.
SqlDependency.Start(connectionString, "ExportedOrdersChangeMessage");
// 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()
{
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
// Release the dependency.
SqlDependency.Stop(connectionString, ExportedHacket);
}
【问题讨论】:
-
你学的是什么教程?您说“这里”,但没有提供链接;除了您的代码/错误(这也意味着您没有提供您使用的 SQL)之外,这些都是死链接。
-
上面添加了教程链接,感谢您忘记在最后添加它。
标签: c# sql-server winforms