本文介绍了是否可以同时使用多个sqldependency?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string connection =我的连接字符串;
string obj1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
Method1();
Method2();
}
private bool DoesUserHavePermission()
{
try
{
SqlClientPermission clientPermission = new
SqlClientPermission(PermissionState.Unrestricted);
clientPermission.Demand();
返回true;
}
捕获
{
返回false;
}
}
private void Method1()
{
try
{
if(!DoesUserHavePermission())
return ;
SqlDependency.Stop(连接);
SqlDependency.Start(connection);
使用(SqlConnection cn = new SqlConnection(connection))
{
if(cn.State == ConnectionState.Close)
cn.Open();
using(SqlCommand cmd = cn.CreateCommand())
{
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText =我的table1命令文本;
SqlDependency dep = new SqlDependency(cmd);
dep.OnChange + = new OnChangeEventHandler(dep_onchange1);
obj1 = cmd.ExecuteScalar()。ToString();
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void dep_onchange1(对象发送者,SqlNotificationEventArgs e)
{
//一些事件
Method1();
SqlDependency dep = sender as SqlDependency;
dep.OnChange - = new OnChangeEventHandler(dep_onchange1);
}
private void Method2()
{
try
{
if(!DoesUserHavePermission())
return;
SqlDependency.Stop(连接);
SqlDependency.Start(connection);
使用(SqlConnection cn = new SqlConnection(connection))
{
if(cn.State == ConnectionState.Close)
cn.Open();
using(SqlCommand cmd = cn.CreateCommand())
{
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText =我的table2命令文本;
SqlDependency dep = new SqlDependency(cmd);
dep.OnChange + = new OnChangeEventHandler(dep_onchange2);
obj2 = cmd.ExecuteScalar()。ToString();
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void dep_onchange2(object sender,SqlNotificationEventArgs e)
{
//某些事件
Method2();
SqlDependency dep = sender as SqlDependency;
dep.OnChange - = new OnChangeEventHandler(dep_onchange2);
}
private void Form1_Closing(object sender,FormClosingEventArgs e)
{
SqlDependency.Stop(connection);
}
我的尝试:
我想同时在两个或三个不同的表中使用SqlDependency.SqlDependency为一个表工作。没有问题。但是不能用于两个或三个不同的表。我可以做什么这样做吗?所以SqlDependency适用于[table1],但它不适用于[table1]和[table2]。只有一个工作。
解决方案
string connection="My connection String"; string obj1; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Method1(); Method2(); } private bool DoesUserHavePermission() { try { SqlClientPermission clientPermission = new SqlClientPermission(PermissionState.Unrestricted); clientPermission.Demand(); return true; } catch { return false; } } private void Method1() { try { if (!DoesUserHavePermission()) return; SqlDependency.Stop(connection); SqlDependency.Start(connection); using (SqlConnection cn = new SqlConnection(connection)) { if(cn.State==ConnectionState.Close) cn.Open(); using (SqlCommand cmd = cn.CreateCommand()) { cmd.Connection=cn; cmd.CommandType = CommandType.Text; cmd.CommandText = "my command text for table1"; SqlDependency dep = new SqlDependency(cmd); dep.OnChange += new OnChangeEventHandler(dep_onchange1); obj1=cmd.ExecuteScalar().ToString(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } void dep_onchange1(object sender, SqlNotificationEventArgs e) { //some events Method1(); SqlDependency dep = sender as SqlDependency; dep.OnChange -= new OnChangeEventHandler(dep_onchange1); } private void Method2() { try { if (!DoesUserHavePermission()) return; SqlDependency.Stop(connection); SqlDependency.Start(connection); using (SqlConnection cn = new SqlConnection(connection)) { if(cn.State==ConnectionState.Close) cn.Open(); using (SqlCommand cmd = cn.CreateCommand()) { cmd.Connection=cn; cmd.CommandType = CommandType.Text; cmd.CommandText = "my command text for table2"; SqlDependency dep = new SqlDependency(cmd); dep.OnChange += new OnChangeEventHandler(dep_onchange2); obj2=cmd.ExecuteScalar().ToString(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } void dep_onchange2(object sender, SqlNotificationEventArgs e) { //some events Method2(); SqlDependency dep = sender as SqlDependency; dep.OnChange -= new OnChangeEventHandler(dep_onchange2); } private void Form1_Closing(object sender, FormClosingEventArgs e) { SqlDependency.Stop(connection); }
What I have tried:
I want to use SqlDependency for two or three different tables at the same time.SqlDependency working for one table.No problem.But not working for two or three different tables.What can I do about this?So SqlDependency working for [table1] but it is not working for [table1] and [table2].İt is working only one.
解决方案
这篇关于是否可以同时使用多个sqldependency?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!