今天对LinqServerModeDataSource进行测试其分页功效,所以做了一个简单的DEMO。
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" ClientInstanceName="grid" DataSourceID="LinqServerModeDataSource1" Width="100%" ClientIDMode="AutoID" KeyFieldName="MEDIA_ID"> <Columns> <dx:GridViewDataTextColumn FieldName="RESOURCE_NAME" VisibleIndex="1"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="MEDIA_KEY" VisibleIndex="2"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="MEDIA_KEY2" VisibleIndex="3"> </dx:GridViewDataTextColumn> </Columns> <SettingsPager NumericButtonCount="20"> </SettingsPager> <Settings ShowFilterRow="true" ShowFilterRowMenu="true"/> </dx:ASPxGridView> <dx:LinqServerModeDataSource ID="LinqServerModeDataSource1" runat="server" ContextTypeName="WebApplication1.Mder_centerEntities" TableName="COMM_MEDIA_INFO"/> </asp:Content> 其中KeyFieldName必须指定。如果只是这样运行时会报错,报错信息如下
Key expression is undefined
解决办法:
aspx文件中LinqServerModeDataSource 加入OnSelecting事件:
<dx:LinqServerModeDataSource ID="LinqServerModeDataSource1" runat="server" ContextTypeName="WebApplication1.Mder_centerEntities" TableName="COMM_MEDIA_INFO" OnSelecting="LinqServerModeDataSource1_Selecting" />
同时在代码中做如下处理:
protected void LinqServerModeDataSource1_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
{
e.KeyExpression = "MEDIA_ID";
}
采用SqlProfiler对数据分页进行了跟踪,发现LinqServerModeDataSource对分页数据加载采用如下手段进行处理
1、数据首次加载
- 先取数据总量
SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT COUNT(1) AS [A1] FROM [dbo].[COMM_MEDIA_INFO] AS [Extent1] ) AS [GroupBy1]
- 从数据表中取64条做为分页用数据(这时取的是64 *注意是64条数据,而不是只取KEY)
2、当选择分页超出已取数据行数时(一页显示10条记录,点击第10页)
- 取数据总量
- 取定量TOP的KEY值 SELECT TOP (128)
[Extent1].[MEDIA_ID] AS [MEDIA_ID]
FROM [dbo].[COMM_MEDIA_INFO] AS [Extent1]
ORDER BY [Extent1].[MEDIA_ID] ASC
- 根据分页取一定数量的数据信息(这时取值为32)
SELECT [Extent1].[MEDIA_ID] AS [MEDIA_ID], [Extent1].[RESOURCE_NAME] AS [RESOURCE_NAME], FROM [dbo].[COMM_MEDIA_INFO] AS [Extent1] WHERE [Extent1].[MEDIA_ID] IN (N'0055f6b2-2e3b-40ce-aaf1-cb0823fc8f8f',N'0059f0d7-df89-4079-b43e-e21e0ffac76c',N'005e0238-158e-4bda-9313-09ebb71db86c',N'005e88ad-68da-4ef6-b320-665e80b2dd8d',N'00604a86-7f94-46fe-9b83-54fb4fbebc68',N'006203d7-ff3b-401f-836e-7f9f9be0752f',N'006241f3-9211-49d8-a2f0-c3fa613718cd',N'0064af47-b122-4512-9658-43585c2cf7a8',N'00672488-6ebe-4d5b-9b06-2639f6900589',N'0068a3ac-2f37-4066-b2ec-d979e0c2dc4d',N'0068fc39-9a30-4805-b5d3-95471e5c2d10',N'0069b167-9aad-4698-962a-c0e327cf0d93',N'0069f792-9c22-41cc-9a82-1e1208ad91b2',N'006a3bfe-d08e-4fe1-8f70-9bacc3e5a2c6',N'006b66c9-91ac-4453-aa0c-0d100d92d098',N'006e0243-987f-49f1-a6ae-d7c1153d0df5',N'006fa965-9904-4b9b-8bee-78b16aa49bd6',N'006fbadd-0fa2-40b2-91ed-6de7ed33c0d1',N'006fd416-2892-435f-b4a7-83ef217cb776',N'006ff6e8-dc1a-49a8-b75c-296c324c965e',N'0071e918-94f6-4005-b22e-80dcfb401625',N'0071fb29-8f85-4ae0-a467-62fb9e46124c',N'007206ed-9899-4d66-b6e6-5bdb0f14d67c',N'0074c52f-c0d6-42f3-8b1a-5694892e18a1',N'00756f2e-61a5-405c-a8cf-22b446b7800f',N'007572f4-c380-4e50-ad29-f7f34e7c19ad',N'00780d78-425a-40c8-83c8-a35b8a2afc7a',N'007884ed-4592-49f0-afde-0ad409c1d69a',N'00797dd6-8e21-40c0-af93-66b86114150b',N'007a5862-3f1c-4794-9a32-fc2fe7bd2cac',N'007ca140-28c6-4094-9b21-3beafe525cfc',N'007e14df-148b-4a2f-ada7-59fb124377b1') ORDER BY [Extent1].[MEDIA_ID] ASC
3、当选择分页超出已取数据行数时(一页显示10条记录,点击第26页)
- 取数据总量
- 取定量TOP的KEY值 SELECT TOP (512)
[Extent1].[MEDIA_ID] AS [MEDIA_ID]
FROM [dbo].[COMM_MEDIA_INFO] AS [Extent1]
ORDER BY [Extent1].[MEDIA_ID] ASC - 根据分页取一定数量的数据信息(这时取值为32)
4、当选择分页超出已取数据行数时(一页显示10条记录,点击第30页)
下面再来看看排序时做的操作
1、首次查询
- 取数据总量
- 取查询条件数据总量
SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT COUNT(1) AS [A1] FROM [dbo].[COMM_MEDIA_INFO] AS [Extent1] WHERE [Extent1].[RESOURCE_NAME] LIKE N'物业%' ) AS [GroupBy1]
- 取数据(由于数据量不大所以SQL语句如下,是不是在大数据量下还有处理暂未测试)
SELECT [Extent1].[MEDIA_ID] AS [MEDIA_ID], [Extent1].[RESOURCE_NAME] AS [RESOURCE_NAME], [Extent1].[MEDIA_KEY] AS [MEDIA_KEY], FROM [dbo].[COMM_MEDIA_INFO] AS [Extent1] WHERE [Extent1].[RESOURCE_NAME] LIKE N'物业%' ORDER BY [Extent1].[MEDIA_ID] ASC