轮询数据库更改:SqlDependency,SignalR很好 [英] Polling for database changes: SqlDependency, SignalR is Good

查看:164
本文介绍了轮询数据库更改:SqlDependency,SignalR很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我需要通过sql依赖项和signalr在db中显示更改数据,那将很好.假设我的交易表经常被很多人更改.假设几秒钟内数据多次更改,那么我想知道通知将如何进入sql依赖类?

it will be good if i need to show change data in db through sql dependency and signalr. suppose my transaction table often changed by many people. suppose in few second data is change many time then i like to know how notification will go to sql dependency class ?

更改数据将在sql依赖项类之前排队吗? sql依赖项类可以在无流量的情况下处理数据更改吗?

change data will be queued before sql dependency class ? sql dependency class can handle data change when huge no of traffic will do the changes ?

在这里,我正在阅读有关SqlDependency&的文章. SignalR.链接为 http://techbrij.com/database-change -notifications-asp-net-signalr-sqldependency

here i was reading a article on SqlDependency & SignalR. the link is http://techbrij.com/database-change-notifications-asp-net-signalr-sqldependency

一些事情我不清楚.

  1. 如何授予IIS订阅查询通知权限?
  2. 请参见本文中的一行.

.

private void dependency_OnChange(object sender, SqlNotificationEventArgs e) {
    JobHub.Show();
}

当数据更改时,将触发 dependency_OnChange 事件,并且JobHub.Show();正在调用

when data will change then dependency_OnChange event will fire and JobHub.Show(); is calling

JobHub is name of class and not static class so i like to know how anyone can call `JobHub.Show();` from out side ??

  1. 什么是GlobalHost类以及何时使用它?
  2. 与文章代码相关的问题.只需转到此链接

http://techbrij.com/database-change-notifications- asp-net-signalr-sqldependency

请参阅视图中的jQuery代码,该代码可获取数据并填充表.第一次页面加载时假设表中有5条记录,因此5条记录将传递给jquery代码,它将仅显示这5条记录,但是当表中任何现有数据发生变化时,将会发生什么?

see the jquery code in view which fetch data and populate table. first time when page load suppose there is 5 records exist in table so 5 records will be passed to jquery code and it will just display those 5 records but when any existing data will be change in table then what will happen ??

仅有更改的行将到达客户端,或者包括更改的数据在内的所有数据都将到达客户端?

the only changed rows will come to client side or all data including changed data will come to client side ?

如果您说只有更改过的数据才能编码,则只需在该链接中看到视频即可.它显示在视频数据中,它一次又一次地更改,并且更改反映在客户端上,但是如果您看到jquery代码,则只需先清空表并再次构建表.所以我的问题是,如果数据发生更改并且只会出现更改数据,那么客户端应显示一行....是的.但在视频中,变化和其他数据也一样.

if you say only changed data will code then just see the video in that link. it is shown in the video data is changed one by one and change is reflecting at client end but if you see the jquery code it just empty the table first and build the table again. so my question is if data change and only change data will come then one row should display at client side.... am i right. but in video the change is showing as well as other data is too.

因此,请您仔细阅读链接文章,然后回答我的问题.谢谢

so please for good sake read the link article once and then answer my question. thanks

请引导我.谢谢

推荐答案

好吧,ManniAT已经接近,但还不是很明显,这里发生的是SQLDependency通知事件是一口气.因此,正如他指出的那样,它将在第一时间触发.您需要删除该处理程序(这样可以防止在以后调用该方法时发生多次触发的情况)并重新添加它,以便再次触发.如果您不想直接从SQLDependency设置方法返回数据,(并且我建议您不要这样做),则可以在需要重建侦听器时调用此方法.

Ok, ManniAT is close, but not quite on the mark, what is going on here is that SQLDependency notification events are a one shot deal. So as he points out it will fire the first time. You need to remove that handler (this prevents the multi-fire scenario when you make a subsequent call to the method) and re-add it so that it will fire again. If you do not want to return your data directly from SQLDependency setup method, (And I recommend that you do not) You can call this method when ever you need to reestablish your listener.

答案: 1)触发通知后,您可以在集线器上调用一个方法来刷新已更改的数据. SqlDependency Notifications应该尽可能具体,并且在触发时,您应该已经知道UI的哪个部分需要更新.

Answers: 1) Once the notification fires, you call a method on the Hub that refreshes the data that has changed. SqlDependency Notifications should be as specific as possible, and when it fires you should already know what part of the UI needs to update.

2)您没有将Hub设置为静态类,因此在调用show方法之前必须先实例化该类的实例,然后才能实例化该类的方法,在本示例中,我认为这是一个静态类,因此这就是为什么它起作用.在这种情况下,我不建议将集线器设置为静态类,而是像此示例一样创建一个单独的集线器跟踪类. http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-server-broadcast-with-signalr-20

2) You did not set your Hub as a static class, and therefore you cannot call methods on the class without first instantiating an instance of the class before calling the show method, in the example I believe it is a static class so that is why it works. Making a hub a static class is not what I would recommend in this care, I would create a separate hub Tracking class like in this example. http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-server-broadcast-with-signalr-20

3)在这种情况下,GlobalHost文件相信是您启动和停止SqlDependencies的Global.asax.

3)GlobalHost File in this case I believe is your Global.asax where you start and stop your SqlDependencies.

对示例的修改:

try
        {
            using (
                var connection =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(@"SELECT [Id]
                                                                        ,[FName]
                                                                        ,[LName]
                                                                        ,[DOB]
                                                                        ,[Notes]
                                                                        ,[PendingReview] 
                                                       FROM [dbo].[Users]",
                    connection))
                {
                    // Make sure the command object does not already have
                    // a notification object associated with it.
                    command.Notification = null;

                    SqlDependency dependency = new SqlDependency(command);

                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                    if (connection.State == ConnectionState.Closed)
                        connection.Open();

                    command.ExecuteReader();
                }
            }
        }
        catch (Exception e)
        {
            throw;
        }
    }

private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{   
    SqlDependency dependency = sender as SqlDependency;
    if (dependency != null) dependency.OnChange -= dependency_OnChange;
    //Recall your SQLDependency setup method here.
    SetupDependency();
    JobHub.Show();
}

希望这对您有所帮助!如果您还有其他问题,请告诉我.

I hope this helps you! If you have any more questions let me know.

这篇关于轮询数据库更改:SqlDependency,SignalR很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆