如何对每个seprate查询使用sqldependency并引发不同的事件

时间:2015-05-21 17:25:13

标签: asp.net-mvc signalr sqldependency

可能这个问题会重复,但我找不到。我正在为我的应用程序使用signalR和sqldependency。我成功执行了一次查询操作。但我会为每个查询编写单独的sqldependency代码。 sqldependency代码在应用程序级别上,或者对于每个查询都是独立的。

1)如何对每个操作使用不同的查询?

2)如何调用不同的onchange事件

 public void RegisterNotification()
    {   string connectionString = ConfigurationManager.ConnectionStrings["FFFConnention"].ConnectionString;

        //We have selected the entire table as the command, so SQL Server executes this script and sees if there is a change in the result, raise the event
     // how to use different query for each operation
        string commandText = @"
                                Select
                                dbo.Notification.UserName
                                From dbo.Notification                                     
                                ";

        //Start the SQL Dependency
        SqlDependency.Start(connectionString);
        using (SqlConnection connection = new SqlConnection(connectionString))
        {

            using (SqlCommand command = new SqlCommand(commandText, connection))
            {
                connection.Open();
                var sqlDependency = new SqlDependency(command);

                NotificationHub hub = new NotificationHub();
                //how to call different onchange event
                sqlDependency.OnChange += new OnChangeEventHandler(hub.sqlDependency_OnChange);

                // NOTE: You have to execute the command, or the notification will never fire.
                command.ExecuteScalar();
            }
        }
    }

0 个答案:

没有答案