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 学生信息表