Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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. 
Posted
Updated 23-Nov-17 23:48pm

1 solution

yes it possible

Maybe this will help. http://www.codeproject.com/KB/database/chatter.aspx
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 24-Nov-17 10:40am    
That can be a comment easily.
RDBurmon 28-Nov-17 1:36am    
by posting this as answer makes more sense to me, there are many open questions in this forum which submitter couldn't mark it as solved because no answer posted there

Trying to manage the queue in better way

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900