简体   繁体   English

asp.net的SqlDependency问题

[英]SqlDependency issue with the asp.net

I am trying get the changed values from the sqlserver using the server_broker feature. 我正在尝试使用server_broker功能从sqlserver获取更改的值。

my code is like below 我的代码如下

 protected void Page_Load(object sender, EventArgs e)
{
    GetData2();

}
private void GetData2()
{
    List<Masa> lst = new List<Masa>();
    using (SqlConnection con = Baglan.Sql)
    {
        string sql = "SELECT [Id],[Ad],[Durum] FROM [dbo].[Masa]";
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            con.Open();
            SqlDependency dependency = new SqlDependency(cmd);
            dependency.OnChange += new OnChangeEventHandler(dependency_OnDataChangedDelegate);

            using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    Masa alt = new Masa
                    {
                        Ad = reader["Ad"].ToString(),
                        Id = reader["Id"].ToString(),
                        Durum = reader["Durum"].ToString()
                    };

                    lst.Add(alt);
                }

                gridMasa.GetStore().DataSource = lst;
                gridMasa.GetStore().DataBind();
            }


        }
    }


}

public void dependency_OnDataChangedDelegate(object sender, SqlNotificationEventArgs e)
{   GetData2();
    SqlDependency dependency = sender as SqlDependency;

    dependency.OnChange -= new OnChangeEventHandler(dependency_OnDataChangedDelegate);

}

when I check the the code via breakpoint which I make some changes on database,It can reach the method called dependency_OnDataChangedDelegate ,but I have no seen changes on my datagrid. 当我通过断点检查代码时,我对数据库进行了一些更改,它可以到达名为dependency_OnDataChangedDelegate的方法,但我的数据网格没有看到变化。 where am I doing wrong?? 我在哪里做错了?

my page source is as below: 我的页面来源如下:

 <ext:ResourceManager runat="server" ID="mymanager"></ext:ResourceManager>
<ext:GridPanel runat="server"  ID="gridMasa" Title="MASALAR" Height="580" Layout="FitLayout" Flex="1"
                        IDMode="Static">

                        <Store>
                            <ext:Store ID="strMasa" runat="server">

                                <Model>
                                    <ext:Model ID="Model1" runat="server">
                                        <Fields>
                                            <ext:ModelField Name="Id" />
                                            <ext:ModelField Name="Ad" />
                                            <ext:ModelField Name="Durum" />
                                        </Fields>
                                    </ext:Model>
                                </Model>
                                <Listeners>
                                    <Exception Handler="Ext.Msg.alert('Products - Load failed', operation.getError());" />
                                </Listeners>
                            </ext:Store>
                        </Store>

                        <ColumnModel ID="ColumnMxodel1" Flex="1" runat="server">
                            <Columns>

                                <ext:RowNumbererColumn ID="Column5" runat="server" Text="Id" 
                                    >
                                </ext:RowNumbererColumn>

                                <ext:Column ID="KisxiId" runat="server" Hidden="true" Text="Id" DataIndex="Id" 
                                    Flex="1">
                                </ext:Column>
                                <ext:Column ID="Coluxmn2w" runat="server" Text="Masa Adı" Flex="1" Align="Center" DataIndex="Ad">

                                </ext:Column>
                                   <ext:Column ID="Column8" runat="server" Text="Durum" Flex="1" Align="Center" DataIndex="Durum">

                                </ext:Column>
                                      <ext:Column ID="Column11" Hidden="true" runat="server" Text="Durum" Flex="1" Align="Center" DataIndex="Durum">

                                </ext:Column>

                            </Columns>
                        </ColumnModel>
                        <SelectionModel>
                            <ext:RowSelectionModel ID="rowSelectioxnModel2" ClientIDMode="Static" runat="server">
                                <SelectedRows>
                                    <ext:SelectedRow RowIndex="0"></ext:SelectedRow>
                                </SelectedRows>


                            </ext:RowSelectionModel>
                        </SelectionModel>

                    </ext:GridPanel>

source code is as below: 源代码如下:

Ext.net.ResourceMgr.init({id:"mymanager",aspForm:"form1"}); Ext.net.ResourceMgr.init({ID: “mymanager”,aspForm: “form1中”}); Ext.onReady(function(){Ext.create("Ext.grid.Panel",{store:{model:Ext.define("App.Model1", {extend: "Ext.data.Model", fields:[{name:"Id"},{name:"Ad"},{name:"Durum"}] }),storeId:"strMasa",autoLoad:true,proxy:{data:[{"Id":"8","Ad":"44448989","Durum":"2"},{"Id":"9","Ad":"MASA 55i","Durum":"1"},{"Id":"12","Ad":"MASA 3","Durum":"1"},{"Id":"44","Ad":"MASA 4","Durum":"1"},{"Id":"45","Ad":"MASA 5","Durum":"1"},{"Id":"46","Ad":"MASA 6","Durum":"-1"},{"Id":"47","Ad":"MASA 7","Durum":"-1"},{"Id":"48","Ad":"MASA 8","Durum":"-1"},{"Id":"49","Ad":"MASA 9","Durum":"1"},{"Id":"51","Ad":"MASA 10","Durum":"2"},{"Id":"52","Ad":"MASA 11","Durum":"-1"},{"Id":"53","Ad":"MASA 12","Durum":"-1"},{"Id":"54","Ad":"MASA 13","Durum":"-1"},{"Id":"55","Ad":"MASA 14","Durum":"-1"},{"Id":"56","Ad":"MASA 15","Durum":"-1"},{"Id":"57","Ad":"MASA 166","Durum":"-1"}], type: 'memory'},listeners:{exception:{fn:function(proxy,response,operation){Ext.Msg.alert('Products - Load failed', operation.getError());}}}},id:"gridMasa",h Ext.onReady(function(){Ext.create(“Ext.grid.Panel”,{store:{model:Ext.define(“App.Model1”,{extend:“Ext.data.Model”,fields:[ {name:“Id”},{name:“Ad”},{name:“Durum”}}}),storeId:“strMasa”,autoLoad:true,proxy:{data:[{“Id”:“8 “,”Ad“:”44448989“,”Durum“:”2“},{”Id“:”9“,”Ad“:”MASA 55i“,”Durum“:”1“},{”Id“ :“12”,“Ad”:“MASA 3”,“Durum”:“1”},{“Id”:“44”,“Ad”:“MASA 4”,“Durum”:“1”}, {“Id”:“45”,“Ad”:“MASA 5”,“Durum”:“1”},{“Id”:“46”,“Ad”:“MASA 6”,“Durum”:“ -1“},{”Id“:”47“,”Ad“:”MASA 7“,”Durum“:” - 1“},{”Id“:”48“,”Ad“:”MASA 8“ ,“Durum”:“ - 1”},{“Id”:“49”,“Ad”:“MASA 9”,“Durum”:“1”},{“Id”:“51”,“Ad” :“MASA 10”,“Durum”:“2”},{“Id”:“52”,“Ad”:“MASA 11”,“Durum”:“ - 1”},{“Id”:“53 “,”Ad“:”MASA 12“,”Durum“:” - 1“},{”Id“:”54“,”Ad“:”MASA 13“,”Durum“:” - 1“},{ “Id”:“55”,“Ad”:“MASA 14”,“Durum”:“ - 1”},{“Id”:“56”,“Ad”:“MASA 15”,“Durum”:“ -1“},{”Id“:”57“,”Ad“:”MASA 166“,”Durum“:” - 1“}],输入:'memory'},听众:{exception:{fn:function (代理,响应,操作){Ext.Msg.alert('Products - Load failed',operation.getError());}}}},id:“gridMasa”,h eight:580,renderTo:"App.gridMasa_Container",flex:1,layout:"fit",title:"MASALAR",columns:{id:"ColumnMxodel1",flex:1,items:[{id:"Column5",xtype:"rownumberer",text:"Id"},{id:"KisxiId",hidden:true,flex:1,dataIndex:"Id",text:"Id"},{id:"Coluxmn2w",flex:1,align:"center",dataIndex:"Ad",text:"Masa Adı"},{id:"Column8",flex:1,align:"center",dataIndex:"Durum",text:"Durum"},{id:"Column11",hidden:true,flex:1,align:"center",dataIndex:"Durum",text:"Durum"}]},selModel:window.App.rowSelectioxnModel2=Ext.create("Ext.selection.RowModel",{proxyId:"rowSelectioxnModel2",selType:"rowmodel",selectedData:[{rowIndex:0}]})});}); 八:580,renderTo: “App.gridMasa_Container”,软硬度:1,布局: “适合”,标题: “MASALAR”,列:{ID: “ColumnMxodel1”,软硬度:1,项目:[{ID: “Column5” ,的xtype: “rownumberer”,文: “ID”},{ID: “KisxiId”,隐藏的:真正的,柔性:1,dataIndex: “ID”,文: “ID”},{ID: “Coluxmn2w”,柔性:1,align:“center”,dataIndex:“Ad”,text:“MasaAdı”},{id:“Column8”,flex:1,align:“center”,dataIndex:“Durum”,text:“Durum “},{ID:” Column11" ,隐藏的:真,挠曲:1,对齐: “中心”,dataIndex: “硬粒”,文字: “硬粒”}]},selModel:window.App.rowSelectioxnModel2 = Ext.create ( “Ext.selection.RowModel”,{proxyId: “rowSelectioxnModel2”,selType: “rowmodel”,selectedData:[{rowIndex位置:0}]})});});

update 更新

this is how enable database(sql server express 2012) which triger every changes happened database. 这是如何启用数据库(sql server express 2012)哪些triger每次更改发生的数据库。

alter database [Your database name here] set enable_broker with rollback immediate 
select name, is_broker_enabled from sys.databases 

在此输入图像描述

All the code you've shown is ASP.Net side code. 您显示的所有代码都是ASP.Net端代码。 The grid is shown on the client, is an HTML element. 网格显示在客户端上,是一个HTML元素。 you need to notify the client of the change. 您需要通知客户有关更改。 HTTP refresh is not a trivial issue, you have to either: HTTP刷新不是一个小问题,您必须:

  • use a timer of the client and periodically poll for changes 使用客户端的计时器并定期轮询更改
  • use WebSockets 使用WebSockets
  • use Server-Sent Events ( Content-Type: text/event-stream ) but this is not supported by IE 使用服务器发送的事件Content-Type: text/event-stream ),但IE不支持此功能

Polling works but it can get taxing on the server, specially with large number of clients. 轮询有效,但它可能会对服务器造成负担,特别是对于大量客户端。 There are countless examples of ASP.Net backed polling using Ajax. 有无数的使用Ajax的ASP.Net支持轮询的例子。

WebSockets requires Windows 8/windows Server 2012 and IIS 8, see Support for WebSockets Protocol WebSockets需要Windows 8 / Windows Server 2012和IIS 8,请参阅支持WebSockets协议

You should probably also look into SignalR , which is an ASP.Net library specifically developer for pushing updates to the client. 您可能还应该查看SignalR ,这是一个ASP.Net库,专门用于将更新推送到客户端的开发人员。

As you can see, my answer does not even touch the topic of Query Notifications . 如您所见,我的答案甚至没有涉及查询通知的主题。 Getting the notification from the DB to the ASP.Net middle tier is only one part of the equation, and SqlDependency is indeed the right answer. 从数据库获取通知到ASP.Net中间层只是方程式的一部分,而SqlDependency确实是正确的答案。 But you're completely missing the second part, the pushing of notification from mid-tier to the browser. 但是你完全错过了第二部分,即从中间层向浏览器推送通知。 You should only notify the browser that the update occurred and the client should refresh. 您应该只通知浏览器更新发生且客户端应该刷新。 Let the refresh load the data using the usual Page_load event. 让刷新使用通常的Page_load事件加载数据。 Use SqlCacheDependency to server the page, this will automatically cache results and refresh the cache on any update. 使用SqlCacheDependency来服务页面,这将自动缓存结果并刷新任何更新的缓存

For an ASP.NET application you have to use the SqlCacheDependency class in name space System.Web.Caching ; 对于ASP.NET应用程序,您必须在名称空间System.Web.Caching使用SqlCacheDependency类; so you don't need the OnChange handler in this scenario. 所以在这种情况下你不需要OnChange处理程序。

This article explains how to implement SqlCacheDependency for an asp.net client: 本文介绍如何为asp.net客户端实现SqlCacheDependency:

Query Notification using SqlDependency and SqlCacheDependency 使用SqlDependency和SqlCacheDependency查询通知

However, you have mentioned you are using SQL Server Express 2012 and according to page Features Supported by the Editions of SQL Server 2012 and similar case in older SQl Server Express edition Using Service Broker with Sql Server Express 2008 , since the SQL Server 2012 Express does not support the broker service , you will be unable to perform Query Notification in SQL Server 2012 Express. 但是,您已经提到过您正在使用SQL Server Express 2012并根据 SQL Server 2012版本支持的页面 功能以及旧SQl Server Express版本中 使用Service Broker和Sql Server Express 2008的类似情况,因为SQL Server 2012 Express 确实如此不支持代理服务 ,您将 无法在SQL Server 2012 Express中执行查询通知。

Edit: this paragraph is incorrect (reckon due to the MSDN page on which is based being misleading). 编辑:此段落不正确(因为MSDN页面基于误导性)。 Express editions support Service Broker and Query Notifications just fine, on all versions since SQL Server 2005. The 'Client Only' comment is better explained on the Express SQL Server Express Features page: 在SQL Server 2005之后的所有版本上,Express版本都支持Service Broker和Query Notifications。在“快速SQL Server Express功能”页面上可以更好地解释“仅客户端”注释:

SQL Server Express supports Service Broker, but direct communication between two SQL Server Express servers is not supported. SQL Server Express支持Service Broker,但不支持两个SQL Server Express服务器之间的直接通信

this restrictions does not affect Query Notifications. 此限制不会影响查询通知。

These references are now merely informational Detecting Changes with SqlDependency , Creating a Query Notification , SqlDependency in an ASP.NET Application . 这些引用现在仅仅是在ASP.NET应用程序中 使用SqlDependency创建查询通知SqlDependency等信息检测更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET中的SqlDependency - SqlDependency in ASP.NET 如何在Asp.Net中实现SQLDependency缓存? - How to implement SQLDependency caching in Asp.Net? 使用SignalR和SqlDependency的ASP.Net MVC 4中的数据库更改更新 - Database change updates in ASP.Net MVC 4 using SignalR and SqlDependency ASP.net MVC OutputCache参数priority - Duration vs SQLDependency - ASP.net MVC OutputCache parameter priority - Duration vs SQLDependency 使用SignalR和SqlDependency的ASP.NET中的数据库更改通知 - Database Change Notifications in ASP.NET using SignalR and SqlDependency 在自定义ASP.NET核心配置提供程序中停止SqlDependency - Stop SqlDependency in custom ASP.NET Core Configuration Provider SqlDependency和ASP.NET MVC应用程序无法完全正常运行 - SqlDependency and asp.net mvc application not fully working 使用signalr和sqldependency在asp.net核心中进行数据库更改通知 - Database change notifications in asp.net core using signalr and sqldependency 在asp.net中触发sqldependency onchange触发,但asp.net控件未刷新 - sqldependency onchange firing in asp.net but asp.net Controls are not refreshing Windows窗体应用程序中的SqlDependency很快,但是在ASP.net MVC应用程序中却很慢 - SqlDependency in Windows Form application is fast, but it is slow in ASP.net MVC application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM