c# - 有什么方法可以对 Azure SQL 数据库更改使用react吗?

标签 c# asp.net azure signalr azure-sql-database

我在应用程序中使用 SignalR 并对我使用 Sql 依赖项的数据库更改使用react

SqlDependency.Start(con);

但我收到以下错误:

此版本的 SQL Server 不支持语句“RECEIVE MSG”

据我了解,Azure SQL 数据库不支持 Service Broker。

除了迁移到Azure VM之外还有什么解决方案吗?

具有 SQL 依赖关系的代码示例:

public class NotificationComponent
{
    public void RegisterNotification(DateTime currentTime)
    {
        string conStr = ConfigurationManager.ConnectionStrings["sqlConString"].ConnectionString;
        string sqlCommand = @"SELECT [ContactID],[ContactName],[ContactNo] from [dbo].[Contacts] where [AddedOn] > @AddedOn";
        using (SqlConnection con = new SqlConnection(conStr))
        {
            SqlCommand cmd = new SqlCommand(sqlCommand, con);
            cmd.Parameters.AddWithValue("@AddedOn", currentTime);
            if (con.State != System.Data.ConnectionState.Open)
            {
                con.Open();
            }
            cmd.Notification = null;
            SqlDependency sqlDep = new SqlDependency(cmd);
            sqlDep.OnChange += sqlDep_OnChange;
            using (SqlDataReader reader = cmd.ExecuteReader())
            {

            }
        }
    }

    void sqlDep_OnChange(object sender, SqlNotificationEventArgs e)
    {
        if (e.Type == SqlNotificationType.Change)
        {
            SqlDependency sqlDep = sender as SqlDependency;
            sqlDep.OnChange -= sqlDep_OnChange;

            var notificationHub = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
            notificationHub.Clients.All.notify("added");
            RegisterNotification(DateTime.Now);
        }
    }

    public List<Contact> GetContacts(DateTime afterDate)
    {
        using (MyPushNotificationEntities dc = new MyPushNotificationEntities())
        {
            return dc.Contacts.Where(a => a.AddedOn > afterDate).OrderByDescending(a => a.AddedOn).ToList();
        }
    }
}

最佳答案

您可以使用 Azure 逻辑应用中 SQL 的“创建项目时”和“修改项目时”触发器来对数据更改使用react。

Azure 逻辑应用中的 SQL 连接器使用轮询机制通过 TIMESTAMP/ROWVERSION 列查询表中的更改。这种数据类型是专门为 SQL 中的此类处理而设计的。轮询查询实质上选择 rowversion 大于上次轮询值的所有行。该行为是可靠的,因为该列由 SQL Server 控制,并且在没有新数据的情况下性能非常快。当有新数据时,性能与简单的行查询相当。

更多信息请阅读this文章。

关于c# - 有什么方法可以对 Azure SQL 数据库更改使用react吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55338264/

相关文章:

c# - 从基础 asp.net 身份用户创建继承用户

asp.net - 超出 session 的请求队列限制

c# - Linq 帮助使用 "Contains"

asp.net - Azure appfabric 缓存、从 Linq 到 SQL 的匿名类的序列化问题

azure - 如何在 Azure 数据工厂中将“直到事件超时”增加到 7 天以上?

c# - 如何从 C# 列表中提取单个列?

c# - 这将是 IDisposable 的有效基类吗

asp.net-mvc - 将 azure MVC 应用程序置于维护模式的巧妙方法

c# - 在 C# 中重用事件处理程序的良好做法

c# - WP7,我能以某种方式在开始菜单中的应用程序磁贴上捕获触摸事件吗?