使用sqldependency的asp.net中的实时更新不起作用 [英] Realtime updates in asp.net using sqldependency not working
本文介绍了使用sqldependency的asp.net中的实时更新不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我试图实时更新数据库中的任何内容时的列表视图.这意味着我需要自动通知数据库中的任何更改,然后列表视图数据在页面上发生更改而无需刷新.因此,我为代理启用了罗斯文数据库并编写了此代码:
Hi all ,
I am trying to update my list view when anything changes in the database in real time.Which means i need to get notified automatically on any change in my database and then the listview data changes on the page without refreshing.So i enabled broker for the northwind database and wrote this code :
protected void Page_Load(object sender, EventArgs e)
{
GetNames();
}
private void GetNames()
{
lbNames.Items.Clear();
SqlDependency.Stop(connect);
SqlDependency.Start(connect);
using (SqlConnection cn = new SqlConnection(connect))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT FirstName, LastName FROM dbo.[Employees]";
cmd.Notification = null;
dep.OnChange += new OnChangeEventHandler(dep_onchange);
cn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
lbNames.Items.Add(dr.GetString(0) + " " + dr.GetString(1));
}
}
}
}
}
public void dep_onchange(object sender, SqlNotificationEventArgs e)
{
GetNames();
SqlDependency dep = sender as SqlDependency;
dep.OnChange -= new OnChangeEventHandler(dep_onchange);
}
但是什么都没有改变.有什么想法吗?
but nothing is changed. So any ideas ?
推荐答案
尝试检查本文
〜单击此处〜
希望对您有帮助
try to Examine this Article
~Click Here~
Hope it helps
这篇关于使用sqldependency的asp.net中的实时更新不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文