1

I wants to monitor multiple table changes using SqlDependency. It works for a single object, but when creating multiple objects, it works only for the last object called in form load.

I am using this DEMO Application.

I also found AggregateCacheDependency can have multiple dependencies on an item, but it is used for ASP.net.

My Windows Forms Code

m_Data = new ChatterData.ChatData();

On Form.Load:

// Hook up event
m_Data.OnNewMessage += new ChatterData.ChatData.NewMessage(OnNewMessage);

// Load existing message
LoadMessages();
private void LoadMessages()
{
    DataTable dt = m_Data.GetMessages();
    dgvName.DataSource = dt ;
}
class ChatData
{
    public delegate void NewMessage();
    public event NewMessage OnNewMessage;
    public Int32 intGroupID;
    /// <summary>
    /// Constructor
    /// </summary>
    public ChatData()
    {
        // Stop an existing services on this connection string
        // just be sure
        SqlDependency.Stop(m_ConnectionString);

        // Start the dependency
        // User must have SUBSCRIBE QUERY NOTIFICATIONS permission
        // Database must also have SSB enabled
        // ALTER DATABASE Chatter SET ENABLE_BROKER
        SqlDependency.Start(m_ConnectionString);

        // Create the connection
        m_sqlConn = new SqlConnection(m_ConnectionString);
    }

    /// <summary>
    /// Destructor
    /// </summary>
    ~ChatData()
    {
        // Stop the dependency before exiting
        SqlDependency.Stop(m_ConnectionString);
    }

    /// <summary>
    /// Retreive messages from database
    /// </summary>
    /// <returns></returns>
    public DataTable GetMessages()
    {
        DataTable dt = new DataTable();

        try
        {
            // Create command
            // Command must use two part names for tables
            // SELECT <field> FROM dbo.Table rather than 
            // SELECT <field> FROM Table
            // Query also can not use *, fields must be designated
            //SqlCommand cmd = new SqlCommand("usp_GetMessages", m_sqlConn);
            SqlCommand cmd = new SqlCommand("[usp_PendingKOTlist]", m_sqlConn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@intKitchenGroupId", intGroupID);
            // Clear any existing notifications
            cmd.Notification = null;

            // Create the dependency for this command
            SqlDependency dependency = new SqlDependency(cmd);

            // Add the event handler
            dependency.OnChange += new OnChangeEventHandler(OnChange);

            // Open the connection if necessary
            if(m_sqlConn.State == ConnectionState.Closed)
                m_sqlConn.Open();

            // Get the messages
            dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return dt;
    }

    /// <summary>
    /// Handler for the SqlDependency OnChange event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void OnChange(object sender, SqlNotificationEventArgs e)
    {
        SqlDependency dependency = sender as SqlDependency;

        // Notices are only a one shot deal
        // so remove the existing one so a new 
        // one can be added
        dependency.OnChange -= OnChange;

        // Fire the event
        if (OnNewMessage != null)
        {
            OnNewMessage();
        }
    }
}

Create multiple class files & objects for SQL dependencies

jjj
  • 4,822
  • 1
  • 16
  • 39
Ranjeet
  • 181
  • 1
  • 3
  • 17
  • 4
    Do you have a question? Should we all read that codeproject article? Please read [how to ask](http://stackoverflow.com/help/how-to-ask) and [edit] your question. – Alex Apr 17 '15 at 13:24
  • 1
    just tell me how to create multiple objects of SQL dependencies in a C# windows application. – Ranjeet Apr 18 '15 at 04:05
  • Are you trying to register multiple `SqlDependency` for a single connection or do you have multiple connections? Also, are you creating multiple `ChatData` objects? – jjj May 08 '15 at 19:12
  • for single database with multiple connection string – Ranjeet May 12 '15 at 09:19

0 Answers0