C# 为什么没有激发SqlDependency.OnChange?
在过去的三天里,我一直在努力解决这个问题,最后我把我的问题放在这里 我正在尝试使用SignalR和SqlDependency构建一个实时应用程序。显然,SQLDependency不起作用。然而,我的信号器工作正常,因为我已经尝试了许多不需要数据库交互的函数 下面是我的代码。是我在引用的 Global.asax.csC# 为什么没有激发SqlDependency.OnChange?,c#,.net,sql-server,signalr,sqldependency,C#,.net,Sql Server,Signalr,Sqldependency,在过去的三天里,我一直在努力解决这个问题,最后我把我的问题放在这里 我正在尝试使用SignalR和SqlDependency构建一个实时应用程序。显然,SQLDependency不起作用。然而,我的信号器工作正常,因为我已经尝试了许多不需要数据库交互的函数 下面是我的代码。是我在引用的 Global.asax.cs public class MvcApplication : System.Web.HttpApplication { NotificationHub objNotificat
public class MvcApplication : System.Web.HttpApplication
{
NotificationHub objNotificationHub;
protected void Application_Start()
{
string connectionString = WebConfigurationManager.AppSettings["SQL"];
// SQL Command Text
string commandText = "SELECT status From tableTask";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
SqlDependency.Start(connectionString);
command.ExecuteReader().Dispose();
objNotificationHub = new NotificationHub();
}
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
objNotificationHub.SendNotifications();
}
}
}
[HubMethodName("sendNotifications")]
public void SendNotifications()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.All.recieveNotification("asbc");
}
NotificationHub.cs(信号机集线器类)
[HubMethodName(“发送通知”)]
公共无效发送通知()
{
IHubContext context=GlobalHost.ConnectionManager.GetHubContext();
上下文。客户。所有。重新通知(“asbc”);
}
My SqlDependency.OnChange事件,即dependency\u OnChange未触发
关于数据库更新
我已经尝试了所有的方法,比如授予权限
ALTER DATABASE MyDB SET ENABLE_代理
还有很多类似的。
但是没有成功
有什么我不知道的吗。另外,是否有任何方法可以检查我的代码是否与SQL Server通信
TIA您没有执行命令,需要执行命令才能获得通知:
SqlDependency.Start(connectionString);
string commandText = "SELECT status From dbo.tableTask"; // don't forget schema here
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
command.ExecuteReader().Dispose();
objNotificationHub = new NotificationHub();
}
确保您了解这些依赖项是如何工作的(例如,在您收到一个通知后,您需要重新注册以获得后续通知)。或者更好,使用一些包装器库,比如一个
您可以使用以下简单示例进行测试:
static void Main(string[] args) {
var cs = "connection string";
using (SqlConnection connection = new SqlConnection(cs))
{
connection.Open();
SqlCommand command = new SqlCommand("select ErrorCode from dbo.Error", connection);
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += OnChange;
SqlDependency.Start(cs);
command.ExecuteReader().Dispose();
}
Console.ReadKey();
}
private static void OnChange(object sender, SqlNotificationEventArgs e) {
Console.WriteLine(e.Info);
}
谢谢Evk。我只是那样做了。不幸的是,没有成功。TBH,我之前也这么做过。但这对我来说不起作用。尽管如此,我只是想知道你说的“你需要重新注册才能获得后续服务”是什么意思。在当前代码中如何实现这一点?我在发布之前验证了它的工作原理,因此它应该可以工作,除非有其他问题。至于重新注册,您只需要在收到一个通知后再次执行所有这些(创建命令、创建依赖项、执行)。首先创建一个简单的控制台应用程序,并确保在没有signar.Evk的情况下,所有这些都能正常工作,我也尝试过一个控制台应用程序,但我也没有成功。数据库更改不会反映在代码方面。事件未被激发。我已经阅读了这篇很好的文章,然后发布了控制台应用程序的代码(最少需要复制),我们将找出问题所在。@MukeshKumar我看到您使用“从tableTask中选择状态”作为命令文本。如何触发新通知,使用什么语句?插入\更新,哪些列?