110,500
社区成员
发帖
与我相关
我的任务
分享
ALTER DATABASE Test SET enable_BROKER;
static void Main(string[] args)
{
InitNotify(System.Configuration.ConfigurationManager.ConnectionString["conn"].ConnectionString);
}
static void InitNotify(string connString)
{
SqlClientPermission perm = new SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted);
try
{
perm.Demand();
Console.WriteLine("可以建立通知");
using (SqlConnection conn = new SqlConnection(connString))
{
SqlDependency.Start(connString);
SqlCommand cmd = new SqlCommand("select item from tab",conn);
SqlDependency dependency = new SqlDependency(cmd);
dependency.OnChange+= GetNotify;
DataTable dt = new DataTable("tab");
conn.Open();
dt.Load(cmd.ExecuteReader());
while (true) ;
SqlDependency.Stop(connString);
conn.Close();
}
}
catch(System.Security.SecurityException )
{
Console.WriteLine("不能建立通知");
}
}
static void GetNotify(Object sender,SqlNotificationEventArgs e)
{
Console.WriteLine("Type={0},Source={1},Inof={2}",e.Type, e.Source, e.Info);
}