创建多个SqlDependencies

时间:2015-04-17 13:09:46

标签: c# sqldependency

我想使用SqlDependency监控多个表格更改。它适用于单个对象,但在创建多个对象时,它仅适用于表单加载中调用的最后一个对象。

我正在使用此DEMO Application

我还发现AggregateCacheDependency可以对项目有多个依赖项,但它用于ASP.net。

我的Windows窗体代码

m_Data = new ChatterData.ChatData();

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();
        }
    }
}

创建多个类文件&amp; SQL依赖项的对象

0 个答案:

没有答案