SQL Server:SQL通知 - 看起来没问题但

时间:2015-11-27 16:08:42

标签: c# sql sql-server notifications sql-server-2014

一个小问题。如何进行sql查询通知工作?

我正在使用SQL 2014,我已经学过一些教程,尤其是this

此外,我在教程中使用了C#,所以它们都是一样的......

似乎一切都好,但我从未收到通知......为什么?我错过了一些参数或其他东西吗?

当应该触发通知事件时,它什么都不会发生..你能帮助我吗?

更新

我的代码:

public delegate void delegateUpd();

private void elapse(object sender, ElapsedEventArgs e)
{
    t.Stop();
    Application.Current.Dispatcher.Invoke(new delegateUpd(doUpdate));
    //MainWindowViewModel.stillBusy = false;
}

private static SqlWatcher SqlQueueWatcher;

private static bool isStartTime = true;

public void SQLServiceStart()
{
    if (isStartTime)
    {
        t = new System.Timers.Timer();
        t.Interval = 1000;
        t.Elapsed += elapse;
    }
    string connS = TmsAdvanceModel.Entities.dbContext.Database.Connection.ConnectionString;// +"Password=" + attivazione.mdlImpostazioni.p.dbPassword + ";";
    if (!connS.Contains("Password"))
        connS += "Password=" + attivazione.mdlImpostazioni.p.dbPassword + ";";
    //Build the command object we want to monitor (don't include a SqlConnection)
    SqlCommand cmd = new SqlCommand();
    cmd = new SqlCommand("SELECT ID FROM dbo.TabNotifiche");
    cmd.CommandType = CommandType.Text;
    //Setup the SQLWatcher
    SqlQueueWatcher = new SqlWatcher(connS, cmd, SqlWatcherNotificationType.Blocking);
    SqlQueueWatcher.OnChange += new SqlWatcher.SqlWatcherEventHandler(QueueSQLWatcher_OnChange);
    SqlQueueWatcher.Start();
}

int counter = 0;
private void QueueSQLWatcher_OnChange(DataSet Result)
{
    try
    {
        R = Result;
        //Do something with the updated DataSet object
        if (!isStartTime)
        {
            if (t.Enabled)
            {
                t.Stop();
                t.Start();
            }
            else
                t.Start();
        }
        else
            isStartTime = false;
        counter++;
        Debug.WriteLine("Counter: " + counter.ToString());
    }
    catch (Exception e) 
    {
        Console.WriteLine(e.ToString());
    }
}

public static void SQLServiceStop()
{
    SqlQueueWatcher.Dispose();
}

是否存在通知无效的特定情况?

1 个答案:

答案 0 :(得分:0)

您是否启用了代理商this

ALTER DATABASE databsename SET ENABLE_BROKER;

你能说出你得到的错误吗?