当前位置:  开发笔记 > 编程语言 > 正文

如何在C#中使用一个函数将SqlDependency和SignalR与2个不同的数据库一起使用?

如何解决《如何在C#中使用一个函数将SqlDependency和SignalR与2个不同的数据库一起使用?》经验,为你挑选了0个好方法。

我有场景使用2个不同的SqlDependencies与2个不同的数据库连接,但调用一个函数.

我希望第一个数据库使用SqlDependency获取更新,然后在第一个数据库更改时同步第二个数据库,那么在第二个数据库更新时我想通过Signalr在客户端加载Kendo Grid的更改,简单的过程正在运行,但是第一次DB1更改它同步DB2然后DB2通知在客户端显示更改,但同样的过程在第二次更改DB1时,SqlDependency调用3次并通知客户端3次,第3次更改DB1其SqlDepency调用6时间或更长时间,表示下次更改后3到SqlDependency调用infite时间:

    EmailHub(DB2 Hub)

        public class EmailHub : Hub
        {
            private static string _connStringDB2 = ConfigurationManager.ConnectionStrings["MyDB2"].ToString();
    
            [HubMethodName("updateRecords")]
            public static void UpdateRecords()
            {
                IHubContext context = GlobalHost.ConnectionManager.GetHubContext();
                context.Clients.All.getUpdates();
            }
    
        }
    

    HMailHub(DB1集线器)

        public class HMailHub : Hub
        {
            private static string _connStringDB1 = ConfigurationManager.ConnectionStrings["MyDB1"].ToString();
    
            [HubMethodName("updateRecords")]
            public static void UpdateRecords()
            {
                IHubContext context = GlobalHost.ConnectionManager.GetHubContext();
                context.Clients.All.getUpdates();
            }
    
        }
    

    GetEmailMessagesSQL(DB2函数)

        public IEnumerable GetEmailMessagesByAccountSQL(string emailid)
        {
            var messages = new List();
    
            // sync hmailDb to LocalDb by EmailAccountId
            HMailServerSync objEmailSync = new HMailServerSync();
            objEmailSync.GetEmailMessagesByAccount(Guid.Parse(emailid));
    
            // stop all Sql dependencies before start new one
            SqlDependency.Stop(_connStringDB1);
            SqlDependency.Stop(_connStringDB2);
    
            //hmailDB service(DB1 sql function call)
            hmailsyncService(emailid);
    
            using (var connection = new SqlConnection(_connString))
            {
                connection.Open();
                using (var command = new SqlCommand(SQL.emailmessagesbyaccount_sql(), connection))
                {
                    command.Parameters.Add(new SqlParameter("@emailaccountid", emailid));
                    command.Notification = null;
                    var dependency = (dynamic)null;
                    SqlDependency.Start(_connStringDB2);
                    dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependencyemailmessage_OnChange);
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    using (var reader = command.ExecuteReader())
                        messages = reader.Cast()
                            .Select(x => new EmailAflAwmMessageDM()
                            {
                                to_msg = x.GetString(0),
                                from_msg = x.GetString(1),
                                subject = x.GetString(2),
                                msg_date = x.GetDateTime(3)
    
                            }).ToList(); 
                 }
                connection.Close();
    
            }
            return messages;
        }
    

    DB2 SqlDependency

        private void dependencyemailmessage_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change)
            {
                EmailHub.UpdateRecords();
            }
        }
    

    HMailDB(DB1 SQL函数)

    public void GetHmailMessagesByAccountSQL(int hmailid)
    {
        using (var connection = new SqlConnection(_connStringDB1))
        {
            connection.Open();
            using (var command = new SqlCommand(SQL.hmailmessages_sql(), connection))
            {
                command.Parameters.Add(new SqlParameter("@messageaccountid", hmailid));
                command.Notification = null;
                var dependency = (dynamic)null;
                SqlDependency.Start(_connStringDB1);
                dependency = new SqlDependency(command);
                dependency.OnChange += new OnChangeEventHandler(dependencyhmailmessage_OnChange);
                if (connection.State == ConnectionState.Closed)
                    connection.Open();
                var reader = command.ExecuteReader();
            }
            connection.Close();
        }
    
    }
    

    DB1 SqlDependency

        private void dependencyhmailmessage_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change)
            {
                EmailHub.UpdateRecords();
            }
        }
    

    客户端代码(Kendo Grid)

       
    @Scripts.Render("~/bundles/signalr")
    跟我搞对象吧
    这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有