• ado.net2.0中的缓存使用SqlDependency类


    private void Form1_Load(object sender, EventArgs e)
    
            {
    
                SqlDependency.Start(connectionString);///启动监听数据表
    
                BindDataGridView();
    
            }
    
            private void BindDataGridView()
    
            {
    
                using (SqlConnection conn = new SqlConnection(connectionString))
    
                {
    
                    using (SqlCommand cmd = new SqlCommand())
    
                    {
    
                        cmd.Connection = conn;
    
                        cmd.CommandType = CommandType.Text;
    
                        cmd.CommandText = "SELECT [OrderID],[ProductID],[UnitPrice],[Quantity],[Discount]  FROM dbo.[Order Details]";
    
                        SqlDependency den = new SqlDependency(cmd, null, 10);//创建一个监听对象实例(cmd : 一定要写,null,是默认,10是设置的10秒)
    
                        den.OnChange += new OnChangeEventHandler(den_OnChange);///注册监听事件
    
                        ////上面两句一定要放到command对象后面,不要下面这个语句里
    
                        using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
    
                        {
    
                            DataTable dt = new DataTable();
    
                            adp.Fill(dt);
    
                            this.dataGridView1.DataSource = dt;
    
                        }
    
                    }
    
                }
    
            }
    
    
    
            void den_OnChange(object sender, SqlNotificationEventArgs e)
    
            {
    
                 ////这个方法要异步执行。
    
                this.dataGridView1.Invoke(new MethodInvoker(delegate { BindDataGridView(); }));
    
            }
    
        }

    ALTER  DATABASE  'databaseName'  SET  ENABLE_BROKER;
    Select  DATABASEpRoPERTYEX('databaseName','IsBrokerEnabled');
    View Code
  • 相关阅读:
    word编号变黑块
    恢复未保存的word
    协方差分析
    SAS字体变大
    可变区组长度--区组随机
    adv and disadv of oncology clinical trial endpoints
    非劣效试验界值确定
    Computer Science
    Compuer Science
    随笔
  • 原文地址:https://www.cnblogs.com/server126/p/2200673.html
Copyright © 2020-2023  润新知