System.Data.SqlClient.SqlDependency类为我们提供了一个关于sql2005的很好的功能 ,就是sql2000时代很多人梦寐以求的查询自动通知。虽然这个东西限制有很多很多,但还是有很实用价值的。
我们先看一个演示例子:
例子中先创建一个sql环境,里面插入了一些数据,然后用.net客户端查询,查询结果出来后再去更改数据库,增加一些记录,同时查看.net客户端的表格是否变化。
1、创建sql测试环境
- use master
- go
- create database dbTest
- go
- use dbTest
- go
- create table test(id int identity(1,1),name varchar(10))
- insert into test(name)select 'aa'
- insert into test(name)select 'bb'
- insert into test(name)select 'cc'
- insert into test(name)select 'dd'
- insert into test(name)select 'ee'
- go
- alter database dbTest set enable_broker
- use master
- go
- create database dbTest
- go
- use dbTest
- go
- create table test(id int identity(1,1),name varchar(10))
- insert into test(name)select 'aa'
- insert into test(name)select 'bb'
- insert into test(name)select 'cc'
- insert into test(name)select 'dd'
- insert into test(name)select 'ee'
- go
- alter database dbTest set enable_broker
use master go create database dbTest go use dbTest go create table test(id int identity(1,1),name varchar(10)) insert into test(name)select 'aa' insert into test(name)select 'bb' insert into test(name)select 'cc' insert into test(name)select 'dd' insert into test(name)select 'ee' go alter database dbTest set enable_broker
2、在windows下用一个form和datagridview来测试
- using System;
- using System.Data;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace WindowsApplication10
- {
- public partial class Form1 : Form
- {
- //拖一个DataGridView到Form中
- public Form1()
- {
- InitializeComponent();
- }
- //变量
- string connString = "server=localhost;uid=sa;pwd=sqlgis;database=dbTest";
- System.Data.SqlClient.SqlConnection conn = null;
- System.Data.SqlClient.SqlCommand command = null;
- //出示化
- private void Form1_Load(object sender, EventArgs e)
- {
- conn = new System.Data.SqlClient.SqlConnection(connString);
- command = conn.CreateCommand();
- command.CommandText = "select id,name from dbo.test where id<>4 order by id desc ";
- SqlDependency.Start(connString);//启动
- GetData();//获取数据
- }
- private void GetData()
- {
- command.Notification = null;//清除
- SqlDependency dependency = new SqlDependency(command);//设置通知
- dependency.OnChange += new OnChangeEventHandler(sqlDependency_OnChange);//通知事件
- using (SqlDataAdapter adapter =new SqlDataAdapter(command)) //查询数据
- {
- System.Data.DataSet ds = new DataSet();
- adapter.Fill(ds,0,3, "test");
- dataGridView1.DataSource = ds.Tables["test"];
- }
- }
- void sqlDependency_OnChange(object sender, SqlNotificationEventArgs e)
- {
- //因为是子线程,需要用invoke方法更新ui
- if (this.InvokeRequired)
- {
- this.Invoke(new OnChangeEventHandler(sqlDependency_OnChange), new object[] { sender, e });
- }
- else
- {
- SqlDependency dependency = (SqlDependency)sender;
- dependency.OnChange -= sqlDependency_OnChange;
- //通知之后,当前dependency失效,需要重新getdata并且设置通知
- GetData();
- }
- }
- private void Form1_FormClosed(object sender, FormClosedEventArgs e)
- {
- //清理现场
- SqlDependency.Stop(connString);
- conn.Close();
- conn.Dispose();
- }
- }
- }
- using System;
- using System.Data;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace WindowsApplication10
- {
- public partial class Form1 : Form
- {
- //拖一个DataGridView到Form中
- public Form1()
- {
- InitializeComponent();
- }
- //变量
- string connString = "server=localhost;uid=sa;pwd=sqlgis;database=dbTest";
- System.Data.SqlClient.SqlConnection conn = null;
- System.Data.SqlClient.SqlCommand command = null;
- //出示化
- private void Form1_Load(object sender, EventArgs e)
- {
- conn = new System.Data.SqlClient.SqlConnection(connString);
- command = conn.CreateCommand();
- command.CommandText = "select id,name from dbo.test where id<>4 order by id desc ";
- SqlDependency.Start(connString);//启动
- GetData();//获取数据
- }
- private void GetData()
- {
- command.Notification = null;//清除
- SqlDependency dependency = new SqlDependency(command);//设置通知
- dependency.OnChange += new OnChangeEventHandler(sqlDependency_OnChange);//通知事件
- using (SqlDataAdapter adapter =new SqlDataAdapter(command)) //查询数据
- {
- System.Data.DataSet ds = new DataSet();
- adapter.Fill(ds,0,3, "test");
- dataGridView1.DataSource = ds.Tables["test"];
- }
- }
- void sqlDependency_OnChange(object sender, SqlNotificationEventArgs e)
- {
- //因为是子线程,需要用invoke方法更新ui
- if (this.InvokeRequired)
- {
- this.Invoke(new OnChangeEventHandler(sqlDependency_OnChange), new object[] { sender, e });
- }
- else
- {
- SqlDependency dependency = (SqlDependency)sender;
- dependency.OnChange -= sqlDependency_OnChange;
- //通知之后,当前dependency失效,需要重新getdata并且设置通知
- GetData();
- }
- }
- private void Form1_FormClosed(object sender, FormClosedEventArgs e)
- {
- //清理现场
- SqlDependency.Stop(connString);
- conn.Close();
- conn.Dispose();
- }
- }
- }
using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication10 { public partial class Form1 : Form { //拖一个DataGridView到Form中 public Form1() { InitializeComponent(); } //变量 string connString = "server=localhost;uid=sa;pwd=sqlgis;database=dbTest"; System.Data.SqlClient.SqlConnection conn = null; System.Data.SqlClient.SqlCommand command = null; //出示化 private void Form1_Load(object sender, EventArgs e) { conn = new System.Data.SqlClient.SqlConnection(connString); command = conn.CreateCommand(); command.CommandText = "select id,name from dbo.test where id<>4 order by id desc "; SqlDependency.Start(connString);//启动 GetData();//获取数据 } private void GetData() { command.Notification = null;//清除 SqlDependency dependency = new SqlDependency(command);//设置通知 dependency.OnChange += new OnChangeEventHandler(sqlDependency_OnChange);//通知事件 using (SqlDataAdapter adapter =new SqlDataAdapter(command)) //查询数据 { System.Data.DataSet ds = new DataSet(); adapter.Fill(ds,0,3, "test"); dataGridView1.DataSource = ds.Tables["test"]; } } void sqlDependency_OnChange(object sender, SqlNotificationEventArgs e) { //因为是子线程,需要用invoke方法更新ui if (this.InvokeRequired) { this.Invoke(new OnChangeEventHandler(sqlDependency_OnChange), new object[] { sender, e }); } else { SqlDependency dependency = (SqlDependency)sender; dependency.OnChange -= sqlDependency_OnChange; //通知之后,当前dependency失效,需要重新getdata并且设置通知 GetData(); } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { //清理现场 SqlDependency.Stop(connString); conn.Close(); conn.Dispose(); } } }
3、启动.net程序,表格中会有3条记录,然后切换到数据库中,继续插入一些数据
- insert into test(name)select 'ff'
- insert into test(name)select 'gg'
- insert into test(name)select 'ff'
- insert into test(name)select 'gg'
insert into test(name)select 'ff' insert into test(name)select 'gg'
4、切换回.net客户端,看看表格是否改变
关于SqlDependency类,很多人无法测试成功,因为它的限制很多,功能可能有待加强,稍微不注意就会让上面的代码陷入死循环。特别要注意的就是command的sql语句问题:
select id,name from dbo.test where id<>4 order by id desc
很遗憾,他只能支持上面这样的简单语句
列明必须写,不能用*,不能用top,不能用函数,包括聚合函数,不能用子查询,包括where后的子查询,不能用外连接,自连接,不能用临时表,不能用变量,不能用视图,不能垮库,而且表名之前必须加类似dbo这样的前缀....具体有多少限制,基本上除了上述的格式或者比上述更简单的格式,其他的都不可以