如何在C#Web应用程序中使用sqltabledependency显示在gridview中插入的新记录 [英] How to display new record inserted in gridview using sqltabledependency in C# web application

查看:100
本文介绍了如何在C#Web应用程序中使用sqltabledependency显示在gridview中插入的新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想使用sqltabledependency显示Gridview中客户端插入的新记录。



先生girdview正在显示数据但不是实时显示,我必须刷新浏览器才能看到新记录。



可以请指导我如何显示实时值在我的gridview中。



我尝试过:



Hi,

I want to use sqltabledependency to display new record inserted by client in Gridview.

Mr girdview is displaying data but not in realtime , i have to refresh my browser to see new record added.

Can any please guide me how to display realtime values in my gridview.

What I have tried:

using (var tableDependency = new SqlTableDependency<Prescription>(con, "PREHEAD"))
       {
           tableDependency.OnChanged += TableDependency_Changed;
           tableDependency.OnError += TableDependency_OnError;
           tableDependency.Start();
           using (SqlConnection sqlcon = new SqlConnection(con))
           {
               sqlcon.Open();
               SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM PREHEAD", sqlcon);

               DataTable dt = new DataTable();
               sqlDa.Fill(dt);
               GridView1.DataSource = dt;
               GridView1.DataBind();

           }
           tableDependency.Stop();

       }
   }

推荐答案

由于ASP.NET中的代码只有运行请求页面并返回它所需的时间,此代码将无法工作。



此外,Web服务器无法将内容推送到浏览器,因为它是一个请求 - 响应系统。



更新必须由另一台服务器监控,并通过其他通信渠道推送到感兴趣的客户端,如SignalR 。



您的客户端代码也必须重写以支持此功能。



这是要做更多的工作和研究,以了解这是如何工作的。谷歌的ASP.NET SignalR用于教程和示例。
Since the code in ASP.NET only runs for the time it takes to request a page and return it, this code won't work.

Also, web servers cannot push content to a browser because it's a "request-response" system.

The updates would have to be monitored for by another server and "pushed" to the interested clients over some other communication channel, like SignalR.

Your client side code would also have to be rewritten to support this.

This is going to be a lot more work and research on your part to understand how this is going to work. Google for "ASP.NET SignalR" for tutorials and examples.


这篇关于如何在C#Web应用程序中使用sqltabledependency显示在gridview中插入的新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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