我有场景使用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 IEnumerableGetEmailMessagesByAccountSQL(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")Tags | 热门标签RankList | 热门文章
- 1如何使1 + 1 = 2而不是1 + 1 = 11
- 2删除父div中的所有文本 - 将字符串保留在子项中
- 3在Access-Control-Request-Headers下添加$ http标头
- 4安装tensorflow时出错设置工具
- 5如何检查一对多映射表中是否存在特定映射
- 6与R的音频比较
- 7SSL认证适用于localhost,但不适用于计算机名称或IP
- 8未知类型名称“ zend_string”
- 9R启动时如何覆盖2GB内存限制
- 10Django编码为utf8无法正常工作
- 11Python中浮点数('Inf')有什么意义?
- 12使用spring-hateoas反序列化JSON(链接+嵌入式容器)的便捷方法是什么?
- 13调试单元测试时,Visual Studio c#不应启动IIS网站
- 14C#Task.WaitAll()如何将对象状态合并为一个?
- 15UIActivityViewController Gmail共享主题和正文相同
- 16在长模式下更改GDT并更新CS
- 17[NSNull length]:无法识别的选择器在键盘按键上发送到实例?
- 18node js函数返回[object Object]而不是字符串值
- 19从AWS SDK,如何获取当前登录的用户名(或IAM用户)?
- 20如何创建动态子域rails