查询更改inSqlCommand时不触发SqlDependency OnChangeEventHandler

时间:2019-05-16 07:38:40

标签: c# signalr sqlcommand sqldependency

我已经使用SqlDependency编写了代码。在将select语句与列列表一起使用时,会触发OnChangeEventHandler事件,但是当我更改select语句以获取行数时,OnChangeEventHandler事件不会触发。

触发OnChangeEventHandler事件的代码:

公共静态类通知 {         静态只读字符串connString = @“数据源= DESKTOP-591MN5Q \ SQLEXPRESS01;初始目录=新建;集成安全性= True;”;         内部静态SqlCommand命令= null;         内部静态SqlDependency相依性= null;

    public static int GetNotificationCount()
    {
        try
        {               
            var messages = new List<tblNotification>(); 
            using (var connection = new SqlConnection(connString))
            {
                connection.Open();
                using (command = new SqlCommand(@"SELECT  [NotificationId],[UserId],[IsSeen],[Message],[CreatedDate],[ActionID] FROM [dbo].[tblNotification]", connection))
                {
                    command.Notification = null;

                    if (dependency == null)
                    {
                        dependency = new SqlDependency(command);
                        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                    }

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

                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        messages.Add(item: new tblNotification
                        {
                            NotificationId = (int)reader["NotificationId"],
                            UserId = reader["UserId"] != DBNull.Value ? (string)reader["UserId"] : "",
                            IsSeen = reader["IsSeen"] != DBNull.Value ? (string)reader["IsSeen"] : "",
                            Message = reader["Message"] != DBNull.Value ? (string)reader["Message"] : "",
                            CreatedDate = reader["CreatedDate"] != DBNull.Value ? (string)reader["CreatedDate"] : "",
                            ActionID = reader["ActionID"] != DBNull.Value ? (string)reader["ActionID"] : "",
                        });
                    }
                }
            }
            return messages.Count;
        }
        catch (Exception ex)
        {

            return 0;
        }
    }

    private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        if (dependency != null)
        {
            dependency.OnChange -= dependency_OnChange;
            dependency = null;
        }
        if (e.Type == SqlNotificationType.Change)
        {
            MyHub.Send();
        }
    }
}

其中不会触发OnChangeEventHandler事件的代码:

公共静态类通知 {     静态只读字符串connString = @“数据源= DESKTOP-591MN5Q \ SQLEXPRESS01;初始目录=新建;集成安全性= True;”;     内部静态SqlCommand命令= null;     内部静态SqlDependency相依性= null;

public static int GetNotificationCount()
{
    try
    { 
        int notificationCt;
        using (var connection = new SqlConnection(connString))
        {
            connection.Open();
            using (command = new SqlCommand(@"SELECT  COUNT([NotificationId]) as NotificationCount FROM [dbo].[tblNotification]", connection))
            {
                command.Notification = null;

                if (dependency == null)
                {
                    dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                }

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

                notificationCt = Convert.ToInt32(command.ExecuteScalar()); 
            }
        }
        return notificationCt;
    }
    catch (Exception ex)
    {

        return 0;
    }
}

private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
    if (dependency != null)
    {
        dependency.OnChange -= dependency_OnChange;
        dependency = null;
    }
    if (e.Type == SqlNotificationType.Change)
    {
        MyHub.Send();
    }
}

}

没有引发异常。我注意到的是

使用(命令=新的SqlCommand(@“ SELECT [NotificationId],[UserId],[IsSeen],[Message],[CreatedDate],[ActionID] FROM [dbo]。[tblNotification]”,连接))< / p>

在那里,在执行GetNotificationCount()函数期间,调试器不会进入dependency_OnChange事件。但是当

使用(命令=新的SqlCommand(@“ SELECT COUNT([NotificationId])作为NotificationCount来自[dbo]。[tblNotification]”,连接))

在那里,调试器在GetNotificationCount()和dependency_OnChange()之间来回移动。

因此,当我在sql表中添加新记录时,不会触发Dependency_OnChange()。

0 个答案:

没有答案