在asp.net中使用sql依赖项不起作用 [英] using sql dependency in asp.net is not working

查看:52
本文介绍了在asp.net中使用sql依赖项不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用asp.net中的sql依赖项来监视何时在数据库中插入新记录.因此当发生这种情况时,我刷新了网格以获取新数据.所以我在sql中启用了代理并编写了以下代码不起作用:

Hi all ,

I am trying to use the sql dependency in asp.net to monitor when a new record is inserted in the database.So when this happens i refresh the grid to get the new data.So i enabled the broker in sql and wrote this code which is not working :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       RefreshGrid()
   End Sub
   Public Sub RefreshGrid()
       SqlDependency.Stop(connectionstring)
       SqlDependency.Start(connectionstring)
       Using cn As SqlConnection = New SqlConnection(connectionstring)
           Using cmd As SqlCommand = cn.CreateCommand()
               cmd.CommandType = CommandType.Text
               cmd.CommandText = "SELECT CarId,CarName FROM dbo.[Cars]"
               cmd.Notification = Nothing
               Dim dep As SqlDependency = New SqlDependency(cmd)
               AddHandler dep.OnChange, AddressOf dep_onchange
               cn.Open()
               Using dr As SqlDataReader = cmd.ExecuteReader()
                   GridView1.DataSource = dr
                   GridView1.DataBind()
               End Using
           End Using
       End Using
   End Sub
   Private Sub dep_onchange(ByVal sender As System.Object, ByVal e As System.Data.SqlClient.SqlNotificationEventArgs)
       RefreshGrid()
       Dim dep As SqlDependency = DirectCast(sender, SqlDependency)
       RemoveHandler dep.OnChange, AddressOf dep_onchange
   End Sub



因此,当iam插入数据库时​​,什么也没发生.



So when iam inserting in the database nothing is happening.

推荐答案

您看过吗? ASP.NET应用程序中的SqlDependency [

这篇关于在asp.net中使用sql依赖项不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆