繁体   English   中英

有没有办法对Azure SQL数据库更改做出反应?

[英]Is there any way to react on Azure SQL Database changes?

我在我的应用程序中使用SignalR并对数据库更改做出反应我使用了Sql Dependency

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 Logic App中的SQL来响应数据更改。

Azure Logic Apps中的SQL连接器使用轮询机制使用TIMESTAMP / ROWVERSION列查询表以进行更改。 此数据类型专门用于SQL中的此类处理。 轮询查询基本上选择rowversion大于上一轮询值的所有行。 该行为是可靠的,因为该列由SQL Server控制,并且在没有新数据的情况下性能非常快。 当有新数据时,性能可与简单行查询相媲美。

欲了解更多信息,请阅读文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM