首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

使用过SQLDependency的进!解决办法

2012-06-07 
使用过SQLDependency的进!想实现以下功能:当数据库中某个表有数据插入的时候,执行后台方法中的代码,于是使

使用过SQLDependency的进!
想实现以下功能:
当数据库中某个表有数据插入的时候,执行后台方法中的代码,
于是使用SQLDependency来实现!
本想数据插入才触发的,没想到不插入数据也总是触发!
贴代码:

C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace ConsoleApplication1{    class Class2    {        private static SqlConnection con = null;        private static SqlCommand com = null;        static void Main(string[] args)        {            string constr = "Data Source=.;Initial Catalog=mailmoniter;Persist Security Info=True;User ID=xxx;Password=xxxxxxxx";            SqlDependency.Start(constr);            con = new SqlConnection(constr);            com = new SqlCommand("SELECT [id],[EmailAccount],[Info],[NowTime] FROM [Mail].[dbo].[_auxUser]", con);            OutputData();            Console.ReadLine();            SqlDependency.Stop(constr);        }        static void depend_OnChange(object sender, SqlNotificationEventArgs e)        {            OutputData();        }        private static void OutputData()        {            com.Notification = null;            SqlDependency depend = new SqlDependency(com);            depend.OnChange += new OnChangeEventHandler(depend_OnChange);            try            {                con.Open();                SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);                while (dr.Read())                {                    Console.WriteLine("{0}   |   {1}", dr[0],dr[1]);                }                dr.Close();                Console.WriteLine();            }            catch(Exception e)            {                Console.WriteLine();            }        }    }}


代码不长,请高手帮忙看看!控制台输入总是不断刷新,但是并没有插入或修改数据!

[解决办法]
帮你顶一下喽。

热点排行