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=sa;pwd=123456;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
gouse 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 guest
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 学生信息表//*****************
update 学生信息表
set 性别 = '女'
where 学生编号 = 'ST0002'
时候应该可以在C#程序中显示变化,我的没变化.
也可以让页面隔多少秒自动刷新(但是WINForm我就不知道怎么写了,可以查查)
还有不明白的就是你的update是谁产生的,
如果是winform,那只需要在update之后调用一次UpdateGrid()而不用timer
如果不是,那期待高手吧
private delegate void GridDelegate(DataTable table);
dataGridView1.Invoke((GridDelegate)delegate(DataTable table)
{
dataGridView1.DataSource = table;
},dt);
dt怎么理解啊。
dataGridView1.Invoke((GridDelegate)delegate(DataTable table)
{
dataGridView1.DataSource = table;
},dt);
dt怎么理解啊。
是SQL数据更新了以后让C#及时的知道吗? 若是这样的话建议使用AJAX技术,这样更好处理一些,每隔一定的时间去读取数据库并返回,若更新了,C#就做相应的处理 用VS2005建一个“一般处理程序”专门用来处理数据库方面,这就跟聊天室功能有点相似,用AJAX技术不断获取数据库里面其他人的聊天内容,不断显示最新进入和退出聊天室的人,所以要让SQL的内容改变通知C#,我觉得这是一个好的办法 或许我把你的问题理解错了:)