您的位置:首页 > 数据库

使用SqlDependency监测数据库

2011-04-01 17:54 218 查看
SqlDependency提供了:对监测的数据库发生变化通知前台程序的功能。

具体使用如下:

public Form1()
{
InitializeComponent();
SqlDependency.Start(conStr);
NewMethod();
}

string conStr = "data source=.;initial catalog=AssetsManageDB;uid=sa;pwd=****";
private void NewMethod()
{
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from TT where flag=1",con);
SqlDependency dependy = new SqlDependency(cmd);
dependy.OnChange += new OnChangeEventHandler(dependy_OnChange);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
MessageBox.Show(dr["message"].ToString());
}
dr.Close();
}
}

void dependy_OnChange(object sender, SqlNotificationEventArgs e)
{
NewMethod();
}

Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=123;User ID=Admin;Data Source=123.mdb
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐