【问题标题】:sql Dependency detect changes of records and send message to clientssql Dependency 检测记录的变化并向客户端发送消息
【发布时间】:2018-09-22 19:22:29
【问题描述】:

我正在尝试使用 sql Dependency 来检查表数据是否已被其他客户端更改,我在这里遵循了教程,但是当我尝试创建队列时,我收到以下错误消息,这是我的 c# 代码和 sql 语句。

编辑 1 抱歉,我忘了在我的第一篇文章中包含教程,这里是教程中的链接,它声明了 quename,但没有说明如何创建它。

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/detecting-changes-with-sqldependency

创建队列 ExportedOrdersChangeMes​​sage 创建服务 ExportedOrdersService ON QUEUE ExportedOrdersChangeMes​​sage ([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


【解决方案1】:

我不知道您的直接问题的答案,但Butterfly Server .NET 检测到数据库(MySQL、Postgres、SQLite、MS SQL Server 等)的更改,并允许基于 SELECT 语句创建动态视图,如果 SELECT 语句发布事件结果改变。有一些注意事项(就像所有数据库操作都必须通过 Butterfly Server .NET 提供的 IDatabase 接口执行)。但是,它与您正在做的事情非常相似,您可能会感兴趣。

【讨论】:

  • 这根本没有帮助。这比什么都垃圾。
猜你喜欢
  • 2020-07-10
  • 2015-04-07
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多