mosaic_born 2021-09-21 21:45 采纳率: 0%
浏览 30
已结题

使用SqlDependency更新Datagridview

通过网上的资料找到了SqlDependency类的使用方法,但没实现成功。望指点迷津~
下面是主程序入口设置对SqlDependency的监听

 static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            string _connStr = "Data Source=LAPTOP-B1KP96US;Initial Catalog=User;Integrated Security=True";
            SqlDependency.Start(_connStr);//传入连接字符串,启动基于数据库的监听
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
            SqlDependency.Stop(_connStr);//传入连接字符串,启动基于数据库的监听

        
    }
    }


然后是应用程序处

        private  void UpdateGrid()
        {
            string _connStr = "Data Source=LAPTOP-B1KP96US;Initial Catalog=User;Integrated Security=True";
            using (SqlConnection connection = new SqlConnection(_connStr))
            {
                //依赖是基于某一张表的,而且查询语句只能是简单查询语句,不能带top或*,同时必须指定所有者,即类似[dbo].[]
                using (SqlCommand command = new SqlCommand("select Channel_ID, Channel_Name, Channel_User, Channel_Route, Channel_Date From[dbo].[Channel]", connection))
                {
                    command.CommandType = CommandType.Text;
                    connection.Open();
                    SqlDependency dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                    SqlDataReader sdr = command.ExecuteReader();
                    Console.WriteLine();
                    while (sdr.Read())

                    {

                        Console.WriteLine("Channel_ID:{0}\tChannel_Name:{1}\tChannel_User:{2}\tChannel_Route:{3}\tChannel_Date:{4}", sdr["Channel_ID"].ToString(), sdr["Channel_Name"].ToString(),sdr["Channel_User"].ToString(), sdr["Channel_Route"].ToString(), sdr["Channel_Date"].ToString());
                    }

                    sdr.Close();
                }
            }
        }


        private  void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change) //只有数据发生变化时,才重新获取并数据

            {
              
                UpdateGrid();
                this.channelTableAdapter.Fill(this.userDataSet.Channel);


            }
        }

不知道哪里出错了,望指点~

  • 写回答

1条回答 默认 最新

  • 关注

    为了使用Push SQL cache dependencies
    ①检查数据库是否设置SQL Server Service Broker
    检查Service Broker是否激活,执行下面语句。

    SELECT name, is_broker_enabled FROM sys.databases
    
    

    如果没有激活,执行下面语句:

    
    ALTER DATABASE MyMovies SET ENABLE_BROKER
    
    

    (下面这是Pollling SQL Cache Dependencies设置,请区分参考)
    如果没设置,请使用ASP.NET Framework 自带的命令行工具aspnet_regsql。
    注意不支持如下:
    local SQL Server 2008 Express or SQL Server 2005 Express
    ② web configuration file是否设置。

    评论

报告相同问题?

问题事件

  • 系统已结题 9月29日
  • 创建了问题 9月21日

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝