SQL 服务器:SQL 通知 - 看起来不错,但是
SQL Server : SQL Notifications - it seems ok but
一个小问题。我怎样才能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();
}
是否存在通知不起作用的特殊情况?
您是否正在启用像 this 这样的代理?
改变数据库数据库名称设置ENABLE_BROKER;
你能说出你遇到的错误吗?
一个小问题。我怎样才能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();
}
是否存在通知不起作用的特殊情况?
您是否正在启用像 this 这样的代理?
改变数据库数据库名称设置ENABLE_BROKER;
你能说出你遇到的错误吗?