CSDN-CSDN社区-.NET技术-.NET技术前瞻

收藏 C#如何实现SQL通知 顶顶[问题点数:100,结帖人:zhangchen124]

楼主发表于:2008-03-12 18:28:20
C# code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Security.Permissions; namespace SQLService { public partial class Form1 : Form { private string connStr = "server=.;uid=user;pwd=user;database=SQL"; private delegate void GridDelegate(DataTable table); private SqlDependency dep; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CanRequestNotifications(); SqlDependency.Start(connStr); UpdateGrid(); } private void UpdateGrid() { string sql = "select * from 学生信息表"; DataTable dt = new DataTable(); using(SqlConnection cn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(sql, cn)) { cn.Open(); dep = new SqlDependency(cmd); dep.OnChange += dep_OnChange; //更新事件. using(SqlDataReader rdr = cmd.ExecuteReader()) { dt.Load(rdr); } } } dataGridView1.Invoke((GridDelegate)delegate(DataTable table) { dataGridView1.DataSource = table; },dt); } private void dep_OnChange(Object sender, SqlNotificationEventArgs e) { MessageBox.Show("接受改变事件"); if (e.Info == SqlNotificationInfo.Invalid) { MessageBox.Show("无效"); return; } UpdateGrid(); return; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { SqlDependency.Stop(connStr); } private bool CanRequestNotifications() { SqlClientPermission per = new SqlClientPermission(PermissionState.Unrestricted); try { per.Demand(); return true; } catch(Exception e) { return false; } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } } /***数据库代码,有误?****/ --建立数据库 create database SQL go use SQL go --指定数据库启用服务代理程序 alter database SQL set ENABLE_BROKER --创建队列 create queue ContactChangeMessage; --创建服务 create service ContactChangeNotification on queue ContactChangeMessage([http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]); --为查询通知设置权限 use SQL GRANT SUBSCRIBE QUERY NOTIFICATIONS TO user update 学生信息表 set 性别 = '' where 学生编号 = 'ST0002' delete from 学生信息表 where 学生编号 = 'ST0001' --****************************************************************************************** create table 学生信息表 ( 学生编号 char(6) primary key, 姓名 char(8) not null, 所在专业 char(18) null, 性别 char(2) not null, 出生日期 smalldatetime not null, 总学分 tinyint null, 备注 varchar(100) null ) insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0001','王大林','计算机应用','','1982-02-03',50,'') insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0002','张小丽','计算机应用','','1981-05-03',50,'') insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0003','李一天','计算机应用','','1980-09-05',45,'') insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0004','吴 严','计算机应用','','1981-11-06',42,'') insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0005','罗小迟','计算机应用','','1982-06-03',50,'') insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0006','孙 伟','电子信息工程','','1985-03-14',45,'') insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0007','马 伟','电子信息工程','','1982-05-07',42,'') insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0008','刘 奇','电子信息工程','','1981-10-08',46,'') insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0009','程 林','电子信息工程','','1983-11-06',43,'') insert into 学生信息表 (学生编号,姓名,所在专业,性别,出生日期,总学分,备注) values('ST0010','赵小刚','电子信息工程','','1984-12-03',41,'') delete from 学生信息表 select * from 学生信息表
回复次数:5
  • my22xo用户头像
  • my22xo
  • (宏远网络)
  • 等 级:
#2楼 得分:30回复于:2008-03-15 12:58:50
#3楼 得分:30回复于:2008-03-16 21:32:33
  • rbwang用户头像
  • rbwang
  • (不轻言放弃)
  • 等 级:
#4楼 得分:20回复于:2008-03-17 00:17:38
  • AptSnail用户头像
  • AptSnail
  • (阳光下的尘埃)
  • 等 级:
#5楼 得分:20回复于:2008-03-18 13:43:41