开发者

SQL Server Notifications - My OnChange does not fire from a windows service

开发者 https://www.devze.com 2023-04-02 13:44 出处:网络
I would like to make use of SQL Server notifications to capture insert/update events at my database within a windows service. I am attempting to use the SQLDependency object. The MSDN articles make th

I would like to make use of SQL Server notifications to capture insert/update events at my database within a windows service. I am attempting to use the SQLDependency object. The MSDN articles make this seem pretty straight forward. So I have created a little example application to give it a try. when making changes to the data into the table it does not raise the OnChange event it would seem. Can someone tell me what I'm missing? Thanks! a sample of my code is below.

private bool CanRequestN开发者_运维技巧otifications()
{
    SqlClientPermission permit = new
    SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted);
    try
    {
        permit.Demand();
        return true;
    }
    catch (System.Exception exc)
    {
        return false;
    }
}

private void NotificationListener()
{
    string mailSQL;
    SqlConnection sqlConn;
    try
    {
        string connString = "Data Source=xyz;Initial Catalog=abc;User ID=sa;Password=******";
        mailSQL = "SELECT * FROM [tbl_test]";

        SqlDependency.Stop(connString);
        SqlDependency.Start(connString);

        sqlConn = new SqlConnection(connString);
        SqlCommand sqlCmd = new SqlCommand(mailSQL, sqlConn);
        this.GetNotificationData();
        evtLog.WriteEntry("Error Stage: NotificationListener" + "Error desc:" + "Message", EventLogEntryType.Error);
    }
    catch (Exception e)
    {
        // handle exception
    }
}

private void GetNotificationData()
{
    DataSet myDataSet = new DataSet();
    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.Notification = null;

    SqlDependency dependency = new SqlDependency(sqlCmd);
    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
    evtLog.WriteEntry("Error Stage: GetNotificationData" + "Error desc:" + "Message", EventLogEntryType.Error);
}

private void dependency_OnChange(object sender,SqlNotificationEventArgs e)
{
    SqlDependency dependency = (SqlDependency)sender;
    dependency.OnChange -= dependency_OnChange;
    this.GetNotificationData();
    evtLog.WriteEntry("Error Stage: dependency_OnChange" + "Error desc:" + "Message", EventLogEntryType.Error);
}

protected override void OnStart(string[] args)
{
    CanRequestNotifications();
    NotificationListener();
}

protected override void OnStop()
{
    SqlDependency dependency = new SqlDependency();
    dependency.OnChange -= dependency_OnChange;
    SqlDependency.Stop(connString);
}


It seems that you're using a new SqlDependency instance for each operation - this won't work in the long run; you ought to a have a reference to a single instance accessible to those parts of the code that need it - this might help in solving your problems.

Also, I can't actually see that you're changing any data, you create the connection and the command, but there is no execution.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号