【发布时间】:2018-12-26 02:45:48
【问题描述】:
我正在使用带有 SqlDependency 的 Signalr。我的代码有效,它向我显示了我想要的实时结果。但问题是它正在使用我新创建的数据库。如果我将数据库更改为旧数据库,SqlDependency 将停止工作,并且无法在我的数据库表上进行更改检测。
下面是我的代码:
#region SignalRMethods
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod(EnableSession = true)]
public GlobalApplicationError[] GetErrorsList()
{
var cs = "Data Source=.;Initial Catalog=NotifyDB;Integrated Security=True";
using (var connection = new SqlConnection(cs))
{
connection.Open();
SqlDependency.Start(cs);
using (SqlCommand command = new SqlCommand(@"SELECT [Form_Name],[Message],[Prepared_By_Date] FROM [GlobalApplicationError]", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new GlobalApplicationError()
{
Form_Name = x["Form_Name"].ToString(),
Message = x["Message"].ToString(),
Prepared_By_Date = Convert.ToDateTime(x["Prepared_By_Date"])
}).ToList().ToArray();
}
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
MyHub.Show();
}
#endregion
如果我更改连接字符串中的数据库,上面的代码完全适用于数据库NotifyDB,但不适用于我现有的eprocure。由于我使用的是 asmx Web 服务,所以我总是更新我的 Web 服务的引用。另外,我在两个数据库上都将 enable_broker 设置为 true。
数据库截图:
NotifyDB
eprocure
输出
请让我知道我在代码中做错了什么。提前致谢。
【问题讨论】:
标签: c# webforms signalr sqldependency