【发布时间】:2015-12-25 04:11:56
【问题描述】:
我的场景是使用 2 个不同的 SqlDependencies 和 2 个不同的数据库连接,但调用一个函数。
我想使用 SqlDependency 获取第一个 DB 的更新,然后在第一个 DB 更改时同步第二个 DB,所以在第二个 DB 更新时,我想通过 Signalr 在客户端 Kendo Grid 加载更改 简单的过程正在工作,但是当第一次 DB1 更改时它同步 DB2 然后 DB2 在客户端通知以显示更改,但在 DB1 中获得第二次更改时也是相同的过程,SqlDependency 调用 3 次并通知客户端 3 次,对于 DB1 中的第 3 次更改,其 SqlDepency 调用 6 次或更多,意味着当 3 次之后的下一次更改时,其 SqlDependency 调用无限时间:
-
EmailHub(DB2 集线器)
public class EmailHub : Hub { private static string _connStringDB2 = ConfigurationManager.ConnectionStrings["MyDB2"].ToString(); [HubMethodName("updateRecords")] public static void UpdateRecords() { IHubContext context = GlobalHost.ConnectionManager.GetHubContext<EmailHub>(); 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<EmailHub>(); context.Clients.All.getUpdates(); } } -
GetEmailMessagesSQL(DB2 函数)
public IEnumerable<EmailAflAwmMessageDM> GetEmailMessagesByAccountSQL(string emailid) { var messages = new List<EmailAflAwmMessageDM>(); // 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<IDataRecord>() .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(); } } -
客户端代码(剑道网格)
<div id="grid"> </div> @Scripts.Render("~/bundles/signalr") <script src="~/signalr/hubs"></script> <script type="text/javascript"> var emailid = '@TempData["DefaultEmailId"]' $(function () { // Declare a proxy to reference the hub. var notifications = $.connection.emailHub; // Create a function that the hub can call to broadcast messages. notifications.client.getUpdates = function () { alert("notification called"); updateGridData(); }; // Start the connection. $.connection.hub.start().done(function () { alert("connection started") updateGridData(); }).fail(function (e) { alert(e); }); function updateGridData() { datasource = new kendo.data.DataSource({ transport: { read: { url: crudServiceBaseUrl + "EmailAflAwmMessage/getMessages/?emailid=" + emailid, dataType: "json", }, update: { url: crudServiceBaseUrl + "EmailAflAwmMessage/Put/", type: "PUT", parameterMap: function (options, operation) { if (operation !== "read" && options.models) { return { models: kendo.stringify(options.models) }; } } }, }, schema: { model: { id: "EmailMessageId", fields: { EmailMessageId: { editable: true, nullable: false, type: "guid" }, subject: { editable: true, nullable: true, type: "string" }, to_msg: { editable: true, nullable: false, type: "string" }, } } } }); $("#grid").kendoGrid({ dataSource: datasource, editable: "popup", toolbar: ["create"], columns: [ { field: "to_msg", title: "to_msg", }, { field: "from_msg", title: "from_msg", }, { field: "subject", title: "subject", }, { field: "msg_date", title: "msg_date", } ], height: "400px", pageable: { refresh: true, pageSizes: true, buttonCount: 5 }, }).data("kendoGrid"); } }); -
Kendo Grid 中的 API 方法使用
public IEnumerable<EmailAflAwmMessageDM> GetMessages(string emailid) { return objEmailSQLFunction.GetEmailMessagesByAccountSQL(emailid); }
我已经在上面详细解释了我的问题。 请指导我解决或建议我任何替代的优化解决方案,感谢您宝贵的时间和精力。 谢谢
【问题讨论】:
标签: c# kendo-grid signalr sqldependency