从Sql Server获取通知

时间:2015-06-18 09:16:51

标签: c# sql .net sql-server service-broker

我想在winforms应用程序中使用SqlDependency类:

    public partial class Form1 : Form
    {
        private int changeCount = 0; 
        private const string statusMessage = "{0} changes have occurred."; 

        private static SqlConnection connection = null;
        private static SqlCommand command = null;
        private static SqlDependency dependency;

        private static SqlCommand command1 = null;
        private static SqlDependency dependency1;

        public Form1()
        {
            InitializeComponent();
            button1.Enabled = CanRequestNotifications();
            this.FormClosed += Form1_FormClosed;
            if (connection == null)
            {
                connection = new SqlConnection(GetConnectionString());
            }

            if (command == null)
            {
                command = new SqlCommand("procCreationUser", connection);
                command.CommandType = CommandType.StoredProcedure;
            }
            dependency = new SqlDependency(command);

            if (connection == null)
            {
                connection = new SqlConnection(GetConnectionString());
            }

            if (command1 == null)
            {
                command1 = new SqlCommand("procSelectionUser", connection);
                command1.CommandType = CommandType.StoredProcedure;
            }
            dependency1 = new SqlDependency(command1);
            GetData();
        }

        private void GetData()
        {

            SqlDependency.Start(GetConnectionString());
            if (connection.State != ConnectionState.Open) connection.Open();
            using (var dr = command.ExecuteReader())
            {
                dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
            }

            using (var dr = command1.ExecuteReader())
            {
                dependency1.OnChange += new OnChangeEventHandler(dependency_OnChange);
            }

        }
        private string GetConnectionString()
        {
            return @"Data Source=PRT-12\SQLEXPRESS; Initial Catalog=TestNotification;Integrated Security=True";
        }


        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            MessageBox.Show("Notification");      
        }

    } 

我创建了两个存储过程:procCreationUser用于创建,procSelectionUser用于选择用户。

当我启动应用程序时,我点击按钮并在数据库中插入一个新行我没有通知!!!!!

  1. 如何修复我的代码?
  2. 这是从Sql Server获取通知的最佳方式吗?

2 个答案:

答案 0 :(得分:1)

我建议你阅读这些内容:

您需要确定以下事件全部发生:

  • 您的查询会创建通知订阅。检查上述文章中提到的Profiler事件,检查sys.dm_qn_subscriptions
  • 检查修改数据是否使订阅无效,请使用上述文章中提到的Profiler事件
  • 检查通知消息是否已发送,特别检查sys.transmission_queue
  • 中的transmission_status
  • 检查您的代码是否已成功设置通知侦听器。您的应用必须每个appdomain呼叫SqlDependency.Start一次,理想情况下应该在appdomain关闭时致电SqlDependency.Stop

收到通知后,请务必检查并尊重SqlNotificationEventArgs arg中的SourceInfoType。并非所有组合都表示成功,通知也可能会立即触发,表明您的订阅存在问题。

此外,还有一小段代码:

  • 您需要在执行查询之前连接SqlDependency

    dependency.OnChange + = new OnChangeEventHandler(dependency_OnChange);   使用(var dr = command.ExecuteReader()){     while(dr.Read()){        ...     }   }

  • 每个appdomain 需要拨打SqlDependency.Start 一次。在显示表单

  • 之前,尝试在Main中调用它

答案 1 :(得分:0)

通常您应先订阅活动dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);,然后才会收到command.ExecuteReader()的更新。
对于两个单独的命令,最好有两个单独的SqlDependency