使用SqlDependency通知用户

时间:2014-10-15 19:24:25

标签: c# winforms sqldependency

我尝试通知连接到同一 SQLServer 的多个应用程序用户某些数据库表发生了更改。我决定使用 SQLDependency 类,因为它似乎是最好的方法,但我不能让它工作。非常感谢您的帮助。

首先,我创建了 SqlWatcher 类来简化使用(信用转到this blog)

public class SqlWatcher : IDisposable
{
    public delegate void SqlWatcherEventHandler(DataSet Result);
    public event SqlWatcherEventHandler OnChange;

    private string ConnectionString;
    private SqlConnection Connection;
    private SqlCommand Command;
    private SqlDataAdapter Adapter;
    private DataSet Result;

    public SqlWatcher(string ConnectionString, SqlCommand Command)
    {
        this.ConnectionString = ConnectionString;
        SqlDependency.Start(this.ConnectionString);
        this.Connection = new SqlConnection(this.ConnectionString);
        this.Connection.Open();
        this.Command = Command;
        this.Command.Connection = this.Connection;
        Adapter = new SqlDataAdapter(this.Command);
    }

    public void ChangeEventWrapper(object state)
    {
        DataSet Result = (DataSet)state;
        OnChange(Result);
    }
    public void Dispose()
    {
        Stop();
    }
    private void RegisterForChanges()
    {
        //Remove old dependency object
        this.Command.Notification = null;
        //Create new dependency object
        SqlDependency dep = new SqlDependency(this.Command);
        dep.OnChange += new OnChangeEventHandler(Handle_OnChange);
        //Save data
        Result = new DataSet();
        Adapter.Fill(Result);
        OnChange(Result);
    }
    public void Start()
    {
        RegisterForChanges();
    }
    public void Stop()
    {
        SqlDependency.Stop(this.ConnectionString);
    }

    private void Handle_OnChange(object sender, SqlNotificationEventArgs e)
    {
        SqlDependency dep = (SqlDependency)sender;
        dep.OnChange -= Handle_OnChange;

        RegisterForChanges();
    }

    public DataSet DataSet
    {
        get { return Result; }
    }
}

从主窗体用户可以将项添加到目标 SqlDependency 命令表中,当发生这种情况时,我希望新窗体弹出新闻。这就是我的主要形式:

public partial class MainForm: Form
{
    private string connectionString = @"data source = .\sqlexpress; initial catalog = Sample1; integrated security = true";
    private static SqlWatcher SqlQueueWatcher;

    public MainForm()
    {
        InitializeComponent();
        InitializeSqlDependency();
    }

    private void InitializeSqlDependency()
    {
        SqlCommand cmd = new SqlCommand();
        cmd = new SqlCommand("SELECT ID, Text FROM dbo.Notifications");
        cmd.CommandType = CommandType.Text;

        //Setup the SQLWatcher
        SqlQueueWatcher = new SqlWatcher(connectionString, cmd);
        SqlQueueWatcher.OnChange += new SqlWatcher.SqlWatcherEventHandler(QueueSQLWatcher_OnChange);
        SqlQueueWatcher.Start();
    }

    private static void QueueSQLWatcher_OnChange(DataSet Result)
    {
        NotificationForm notificationForm = new NotificationForm(Result);
        notificationForm.Show();
    }
    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        SqlQueueWatcher.Dispose();
    }

我预计这会以任何用户向通知数据库添加数据的方式工作,其他用户会通过 NotificationForm 获得通知。谁能告诉我我做错了什么?我现在已经试着弄清楚了两天。

提前致谢!

P.S。如果您更好地了解相同的通知概念,请随意表达自己。

0 个答案:

没有答案