I'm using SqlDependency to control my cache. I want to use it to monitor several tables (around 10). There should be one SqlDependency per watched table.
Should I create for each of them code like that:
public void CreateDependency_Table()
{
if (connectionStringSettings != null)
{
using (SqlConnection conn = new SqlConnection(connectionStringSettings.ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT id from dbo.Table", conn))
{
cmd.Notification = null;
SqlDependency sqlDependency = new SqlDependency(cmd);
sqlDependency.OnChange += new OnChangeEventHandler(sqlDep_Table_OnChange);
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
}
}
}
}
}
and:
private void sqlDep_Table_OnChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency = (SqlDependency)sender;
dependency.OnChange -= sqlDep_Table_OnChange;
MyCacheWhatever.Clear();
//Re-attach dependency
CreateDependency_Table();
}
or can I reuse something between them? Like connection?
Is this the preferred way of setting multiple notifications?