数据库缓存依赖

本文详细介绍了如何在SQL Server中使用aspnet_regsql工具建立和删除依赖,以及如何通过SQL语句开启和关闭Broker功能。此外,还展示了如何利用SqlDependency进行实时数据更新通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

建立依赖

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

aspnet_regsql -C "Data Source=xxx;Initial Catalog=basic_emrcp;User ID=xx;Password=xxx;" -ed -et -t "table_name"  

开启broker

ALTER DATABASE [basic_emrcp] SET NEW_BROKER WITH ROLLBACK IMMEDIATE;

GO
ALTER DATABASE [basic_emrcp] SET DISABLE_BROKER ;

GO

删除依赖

aspnet_regsql -C "Data Source=192.168.101.53;Initial Catalog=basic_emrcp;User ID=sa;Password=test;" -dd 

 

        private static void UpdateGrid()
        {
            using (SqlConnection connection = new SqlConnection(_connStr))
            {
                //依赖是基于某一张表的,而且查询语句只能是简单查询语句,不能带top或*,同时必须指定所有者,即类似[dbo].[]
                using (SqlCommand command = new SqlCommand("select ID,UserID,[Message] From [dbo].[Messages]", 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("Id:{0}\tUserId:{1}\tMessage:{2}",sdr["ID"].ToString(), sdr["UserId"].ToString(), 
 
sdr["Message"].ToString());
                   }
                   sdr.Close();
                }
            }
        }
 
 
        private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            UpdateGrid();
        }

 

转载于:https://www.cnblogs.com/CoreXin/p/9323480.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值