【问题标题】:Getting notification from Sql Server从 Sql Server 获取通知
【发布时间】:2015-06-18 09:16:51
【问题描述】:

我想在我的 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 获取通知的最佳方式吗?

【问题讨论】:

  • 你需要整理你的代码,它有点到处都是。
  • @artm 请看我的编辑
  • 您是否在数据库中启用了 Service Broker。
  • @DevD 是的,它已激活
  • 你还有一些不必要的代码,为什么你需要一个using reader 围绕dependency.onChange()?看起来您的编辑还删除了更新数据的代码?

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


【解决方案1】:

我建议您阅读以下内容:

您需要确定以下事件都发生了:

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

收到通知后,请确保检查并遵守 SqlNotificationEventArgs arg 中的 Source、Info 和 Type。并非所有组合都表示成功,通知也可能会立即触发,表明您的订阅存在问题。

另外,关于您的代码的小花絮:

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

    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); 使用 (var dr = command.ExecuteReader()) { 而(博士读()){ ... } }

  • 您需要调用SqlDependency.Start 每个应用程序域一次。在显示表单之前尝试在 Main 中调用它

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 请查看我的编辑,即使我这样更改我的代码,我也没有收到任何通知
    • 您仍然订阅太晚了。 dependency1.OnChange += new OnChangeEventHandler(dependency_OnChange); 应该在 Form1() 方法中。
    • 在构造函数中调用的Getdata方法中执行!!
    • 我已经稍微编辑了我的评论,现在可能更有意义了。
    猜你喜欢
    • 2012-08-25
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2012-07-18
    相关资源
    最近更新 更多