【发布时间】:2017-01-16 05:28:28
【问题描述】:
这是我的代码:
Public Sub RegisterNotification()
Dim conStr As String = ConfigurationManager.ConnectionStrings("sqlConString").ConnectionString
Dim sqlCommand As String = "SELECT [ID],[Description], [CreateDate], [NoteTypeID] from [note].[Notes]"
Using con As New SqlConnection(conStr)
Dim cmd As New SqlCommand(sqlCommand, con)
If con.State <> System.Data.ConnectionState.Open Then
con.Open()
End If
cmd.Notification = Nothing
Dim sqlDep As New SqlDependency(cmd)
AddHandler sqlDep.OnChange, AddressOf sqlDep_OnChange
Using reader As SqlDataReader = cmd.ExecuteReader()
End Using
End Using
End Sub
Private Sub sqlDep_OnChange(sender As Object, e As SqlNotificationEventArgs)
If e.Type = SqlNotificationType.Change Then
Dim sqlDep As SqlDependency = TryCast(sender, SqlDependency)
'Dim id
RemoveHandler sqlDep.OnChange, AddressOf sqlDep_OnChange
If e.Info = 1 Then
Dim notificationHub = GlobalHost.ConnectionManager.GetHubContext(Of NotificationHub)()
notificationHub.Clients.All.notify("added")
RegisterNotification()
'GetNotificationList(0, 1)
End If
End If
End Sub
Global.asax 代码:
Protected Sub Application_Start()
Try
Dim _obj As New DAL.sap.Register
_obj.Register()
AreaRegistration.RegisterAllAreas()
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
RouteConfig.RegisterRoutes(RouteTable.Routes)
BundleConfig.RegisterBundles(BundleTable.Bundles)
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "webpages_UserProfile", "UserId", "UserName", True)
SqlDependency.Start(con)
Catch ex As Exception
End Try
End Sub
Protected Sub Session_Start(sender As Object, e As EventArgs)
Try
Dim NC As New NotificationComponent()
NC.RegisterNotification()
Catch ex As Exception
End Try
End Sub
Protected Sub Application_End()
Try
SqlDependency.Stop(con)
Catch ex As Exception
End Try
End Sub
启动代码:
<Assembly: OwinStartupAttribute(GetType(www.Startup))>
命名空间 www 部分公开课启动 公共子配置(app As IAppBuilder) app.MapSignalR() 结束子 结束类 结束命名空间
我的问题是 sqldep_onchange 并不总是触发。有时它工作得很好,但有时什么也没有发生。它是一个错误或类似的东西吗?我做错了什么?
【问题讨论】:
标签: vb.net onchange sqldependency