.NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录

本文演示了如何使用DLINQ(LINQtoSQL)结合GridView和ObjectDataSource控件实现大数据量分页,并介绍了延迟执行和日志记录的方法。通过Northwind数据库示例,展示了分页查询的T-SQL生成过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录



作者:webabcd


介绍
以Northwind为示例数据库,DLINQ(LINQ to SQL)之结合GridView控件和ObjectDataSource控件演示大数据量分页,同时介绍延迟执行和日志记录


示例
PagingAndLogging.aspx

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="PagingAndLogging.aspx.cs"
        Inherits="LINQ_DLINQ_PagingAndLogging" Title="大数据量分页、延迟执行和日志记录" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <asp:GridView ID="gvProduct" runat="server" DataSourceID="odsProduct" AllowPaging="True" PageSize="5">
        </asp:GridView>
        <asp:ObjectDataSource ID="odsProduct" runat="server" EnablePaging="True" SelectCountMethod="GetProductCount"
                SelectMethod="GetProduct" TypeName="PagingAndLogging">
                <SelectParameters>
                        <asp:Parameter Name="startRowIndex" Type="Int32" DefaultValue="0" />
                        <asp:Parameter Name="maximumRows" Type="Int32" DefaultValue="10" />
                </SelectParameters>
        </asp:ObjectDataSource>
</asp:Content>

 

PagingAndLogging.cs

InBlock.gif using System;
InBlock.gif using System.Data;
InBlock.gif using System.Configuration;
InBlock.gif using System.Linq;
InBlock.gif using System.Web;
InBlock.gif using System.Web.Security;
InBlock.gif using System.Web.UI;
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.Web.UI.WebControls.WebParts;
InBlock.gif using System.Web.UI.HtmlControls;
InBlock.gif using System.Xml.Linq;
InBlock.gif
InBlock.gif using System.ComponentModel;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.IO;
InBlock.gif using DAL;
InBlock.gif
/// <summary>
/// PagingAndLogging 的摘要说明
/// </summary>
InBlock.gif[DataObject]
InBlock.gif public class PagingAndLogging
InBlock.gif{
InBlock.gif        [DataObjectMethod(DataObjectMethodType.Select, true)]
InBlock.gif         public List<Products> GetProduct( int startRowIndex, int maximumRows)
InBlock.gif        {
InBlock.gif                NorthwindDataContext ctx = new NorthwindDataContext();
InBlock.gif
InBlock.gif                 // System.Data.Linq.DataContext的记录日志的功能
InBlock.gif                StreamWriter sw = new StreamWriter(HttpContext.Current.Request.PhysicalApplicationPath + "Log.txt", true);
InBlock.gif                ctx.Log = sw;
InBlock.gif
InBlock.gif                var products = (from p in ctx.Products
InBlock.gif                                                select p).Skip(startRowIndex).Take(maximumRows);
InBlock.gif
InBlock.gif                 // products实现了IQueryable<T>接口
InBlock.gif                 // 所以可以用如下方法从中获取DbCommand
InBlock.gif                System.Data.Common.DbCommand cmd = ctx.GetCommand(products);
InBlock.gif                 string commandText = cmd.CommandText;
InBlock.gif                 foreach (System.Data.Common.DbParameter param in cmd.Parameters)
InBlock.gif                {
InBlock.gif                         string parameterName = param.ParameterName;
InBlock.gif                         object value = param.Value;
InBlock.gif                }
InBlock.gif
InBlock.gif                 // 延迟执行(Deferred Execution)
InBlock.gif                 // products实现了IEnumerable<T>接口
InBlock.gif                 // IEnumerable<T>接口的一个特性是,实现它的对象可以把实际的查询运算延迟到第一次对返回值进行迭代(yield)的时候
InBlock.gif                 // ToList()之前,如果是LINQ to SQL的话,那么就可以通过products.ToString()查看LINQ生成的T-SQL
InBlock.gif                 // ToList()后则执行运算
InBlock.gif                var listProducts = products.ToList();
InBlock.gif
InBlock.gif                 // 执行运算后System.Data.Linq.DataContext会记录日志,所以应该在执行运算后Close掉StreamWriter
InBlock.gif                sw.Flush();
InBlock.gif                sw.Close();
InBlock.gif
InBlock.gif                 return listProducts;
InBlock.gif        }
InBlock.gif
InBlock.gif         public int GetProductCount( int startRowIndex, int maximumRows)
InBlock.gif        {
InBlock.gif                NorthwindDataContext ctx = new NorthwindDataContext();
InBlock.gif
InBlock.gif                StreamWriter sw = new StreamWriter(HttpContext.Current.Request.PhysicalApplicationPath + "Log.txt", true);
InBlock.gif                ctx.Log = sw;
InBlock.gif
InBlock.gif                 // Count查询操作符(不延迟) - 返回集合中的元素个数
InBlock.gif                 int c = (from p in ctx.Products
InBlock.gif                                 select 0).Count();
InBlock.gif
InBlock.gif                sw.Flush();
InBlock.gif                sw.Close();
InBlock.gif
InBlock.gif                 return c;
InBlock.gif        }
InBlock.gif}

 

通过查看日志可以发现,单击第1页时DLINQ生成的T-SQL语句如下

SELECT TOP 5 [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1

SELECT COUNT(*) AS [value]
FROM [dbo].[Products] AS [t0]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1

 

通过查看日志可以发现,单击第10页时DLINQ生成的T-SQL语句如下

SELECT TOP 5 [t1].[ProductID], [t1].[ProductName], [t1].[SupplierID], [t1].[CategoryID], [t1].[QuantityPerUnit], [t1].[UnitPrice], [t1].[UnitsInStock], [t1].[UnitsOnOrder], [t1].[ReorderLevel], [t1].[Discontinued]
FROM (
         SELECT ROW_NUMBER() OVER ( ORDER BY [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]) AS [ROW_NUMBER], [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
         FROM [dbo].[Products] AS [t0]
        ) AS [t1]
WHERE [t1].[ROW_NUMBER] > @p0
-- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [45]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1

SELECT COUNT(*) AS [value]
FROM [dbo].[Products] AS [t0]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1

 

本文出自 “webabcd” 博客,请务必保留此出处http://webabcd.blog.51cto.com/1787395/345013

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值