监听数据库是否发生变化,运用Service Broker、SqlDependency,在.net下实现如下:
private bool flag = false;
private void button1_Click(object sender, EventArgs e)
{
string str = "Data Source=DATASERVER;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=sa";
using( SqlConnection conn = new SqlConnection(str))
using (SqlCommand cmd = new SqlCommand("select * from TableOperate", conn))
{
SqlDependency.Start(str);
SqlDependency depend = new SqlDependency(cmd);
depend.OnChange += new OnChangeEventHandler(depend_OnChange);
conn.Open();
if (flag)
{
cmd.ExecuteNonQuery();