如何使用Sqldependency自动更新页面加载
本文关键字:加载 更新 何使用 Sqldependency | 更新日期: 2023-09-27 18:26:00
我正试图从一个web表单类创建并运行一个sqldependency方法,其中我有一个"UpdateMethod",我使用窗口表单作为引用来实现它
然而,当数据库发生变化时,该方法的web表单版本不会自动更新客户端上的页面加载,而窗口表单会更新。
窗口形式方法版本:
delegate void GridDelegate(DataTable table);
private void UpdateGrid()
{
string sql = "SELECT * FROM [dbo].[User]";
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(connectionstring))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
dep = new SqlDependency(cmd); //Passing Command to SQL dependency
dep.OnChange += dep_OnChange;
using (SqlDataReader rdr = cmd.ExecuteReader())
{
dt.Load(rdr);
}
}
}
dataGridView1.Invoke((GridDelegate)delegate(DataTable table)
{ dataGridView1.DataSource = table; }, dt);
}
Web表单版本的方法:
private void UpdateGrid()
{
string sql = "SELECT * FROM [dbo].[User] order by uploadDate desc";
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(GetConnectionString()))
{
try
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
SqlDependency dep;
dep = new SqlDependency(cmd); //Passing Command to SQL dependency
dep.OnChange += dep_OnChange;
using (SqlDataReader rdr = cmd.ExecuteReader())
{
dt.Load(rdr);
// GridView1.DataSource = rdr;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
我试着添加。。Catch语句在web表单版本中,但我没有从中得到任何错误。请建议我如何进一步测试此方法,以解决此问题。任何提示/建议都将不胜感激。感谢
这是一个实体模型。我没有sql服务器可以使用;因此,我已经完成了计时器(线程)的示例。我已将SignalR用于此目的。确保浏览器也有点现代化(IE10+、Chrome、Mozilla等支持websocket)。
- 使用构造函数创建集线器类
构造函数初始化并激发计时器线程。线程执行的代码指示集线器在客户端(即连接到集线器的所有浏览器)上调用特定的js方法。
这个特殊的js方法将承载刷新更新面板中的gridview的逻辑。
- 编写必要的javascript来进行刷新(aspx)
aspx页面设计简单。它有一个更新面板,里面有一个绑定到sqldatasource的网格视图,还有一个链接按钮,其主要职责是刷新网格数据(服务器端)。这方面的代码非常简单,稍后将在下面显示。您可以注意到,为了使刷新工作,我们正在模拟手动单击LinkButton控件(托管在udpatepanel中)。
- 编写必要的逻辑以连接hub(aspx)
这是连接到集线器的代码,并编写在收到服务器消息时应该调用的js方法。
还要确保将owin启动类文件添加到项目中。请参阅SignalR教程了解更多信号器内容。
数据中心.cs
using Microsoft.AspNet.SignalR;
using System.Threading;
using System.Diagnostics;
namespace WebApp.SignalR.GridUpdatePanel
{
public class DataHub : Hub
{
public DataHub()
{
Debug.Print("Ctor executed...");
Timer tmr = new Timer(new TimerCallback(this.RefreshThread), null, 1000, 5000);
}
//Method which invokes the client js code...
public void Refresh()
{
Clients.All.refreshData();
}
public void RefreshThread(object obj)
{
Debug.Print("RefreshThread Called...");
Refresh();
}
}
}
SignalRPage.aspx
<%@ Page Title="SignalR Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SingalRPage.aspx.cs" Inherits="WebApp.SignalR.GridUpdatePanel.SingalRPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphPageHeadScripts" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphPageBody" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="PName" HeaderText="PName" SortExpression="PName" />
<asp:BoundField DataField="PAge" HeaderText="PAge" SortExpression="PAge" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PersonsDbConnectionString1 %>" DeleteCommand="DELETE FROM [Persons] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Persons] ([Id], [PName], [PAge]) VALUES (@Id, @PName, @PAge)" ProviderName="<%$ ConnectionStrings:PersonsDbConnectionString1.ProviderName %>" SelectCommand="SELECT [Id], [PName], [PAge] FROM [Persons]" UpdateCommand="UPDATE [Persons] SET [PName] = @PName, [PAge] = @PAge WHERE [Id] = @Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="PName" Type="String" />
<asp:Parameter Name="PAge" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="PName" Type="String" />
<asp:Parameter Name="PAge" Type="Int32" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" CssClass="btn btn-default" OnClick="LinkButton1_Click">Refresh</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cphPageBottomScripts" runat="server">
<script src="Scripts/jquery.signalR-2.0.0.min.js"></script>
<script src="signalr/hubs"></script>
<script>
function refreshGrid() {
console.log('grid refresh');
var lnk = document.getElementById('<%= LinkButton1.ClientID %>');
lnk.click();
}
</script>
<script>
$(function () {
var chat = $.connection.dataHub;
chat.client.refreshData = function () {
refreshGrid();
};
$.connection.hub.start().done(function () {
console.log('hub started...');
});
});
</script>
</asp:Content>
在这里,我使用了以下母版页。
Site.Master
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApp.SignalR.GridUpdatePanel.Site" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="/Content/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<asp:ContentPlaceHolder ID="cphPageHeadScripts" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<asp:ContentPlaceHolder ID="cphPageBody" runat="server"></asp:ContentPlaceHolder>
</form>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="/Scripts/jquery-1.9.1.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="/Scripts/bootstrap.min.js"></script>
<asp:ContentPlaceHolder ID="cphPageBottomScripts" runat="server"></asp:ContentPlaceHolder>
</body>
</html>
最后,主aspx页面的代码如下:
using System;
using System.Diagnostics;
namespace WebApp.SignalR.GridUpdatePanel
{
public partial class SingalRPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Debug.Print("LinkButton1_Click");
Label1.Text = DateTime.Now.ToLongTimeString();
GridView1.DataBind();
}
}
}