Можно ли одновременно использовать более одной sqldependency?
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); }
Что я уже пробовал:
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.