日期:2014-05-18 浏览次数:20930 次
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(); } } } }