DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
2011-08-30 13:26
330 查看
介绍 以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 using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; using System.ComponentModel; using System.Collections.Generic; using System.IO; using DAL; /**//// <summary> /// PagingAndLogging 的摘要说明 /// </summary> [DataObject] public class PagingAndLogging { [DataObjectMethod(DataObjectMethodType.Select, true)] public List<Products> GetProduct(int startRowIndex, int maximumRows) { NorthwindDataContext ctx = new NorthwindDataContext(); // System.Data.Linq.DataContext的记录日志的功能 StreamWriter sw = new StreamWriter(HttpContext.Current.Request.PhysicalApplicationPath + "Log.txt", true); ctx.Log = sw; var products = (from p in ctx.Products select p).Skip(startRowIndex).Take(maximumRows); // products实现了IQueryable<T>接口 // 所以可以用如下方法从中获取DbCommand System.Data.Common.DbCommand cmd = ctx.GetCommand(products); string commandText = cmd.CommandText; foreach (System.Data.Common.DbParameter param in cmd.Parameters) { string parameterName = param.ParameterName; object value = param.Value; } // 延迟执行(Deferred Execution) // products实现了IEnumerable<T>接口 // IEnumerable<T>接口的一个特性是,实现它的对象可以把实际的查询运算延迟到第一次对返回值进行迭代(yield)的时候 // ToList()之前,如果是LINQ to SQL的话,那么就可以通过products.ToString()查看LINQ生成的T-SQL // ToList()后则执行运算 var listProducts = products.ToList(); // 执行运算后System.Data.Linq.DataContext会记录日志,所以应该在执行运算后Close掉StreamWriter sw.Flush(); sw.Close(); return listProducts; } public int GetProductCount(int startRowIndex, int maximumRows) { NorthwindDataContext ctx = new NorthwindDataContext(); StreamWriter sw = new StreamWriter(HttpContext.Current.Request.PhysicalApplicationPath + "Log.txt", true); ctx.Log = sw; // Count查询操作符(不延迟) - 返回集合中的元素个数 int c = (from p in ctx.Products select 0).Count(); sw.Flush(); sw.Close(); return c; } } 通过查看日志可以发现,单击第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
相关文章推荐
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- Linq to sql:延迟执行
- 步步为营VS 2008 + .NET 3.5(9) - DLINQ(LINQ to SQL)之执行SQL语句的添加、查询、更新和删除
- 光脚丫学LINQ(015):使用LINQ to SQL可以执行的操作
- 直接执行查询和命令(LINQ TO SQL)
- 【小菜日志】用LINQ的DataContext.Log 属性来记录LINQ TO语句操作日志
- Linq To Sql模式中自动生成T-SQL增删改操作系列~PropertyChanged事件实现在子类中记录属性的变化,在基类中进行统一处理
- linq to sql取出随机记录/多表查询/将查询出的结果生成xml
- (015):使用LINQ to SQL可以执行的操作
- 简单骗过DataContext,统计Linq to SQL执行查询的次数
- 步步为营VS 2008 + .NET 3.5(9) - DLINQ(LINQ to SQL)之执行SQL语句的添加、查询、更新和删除
- LINQ TO SQL:动态执行带有有参数的存储过程
- LINQ TO SQL 怎样 执行存储过程并返回存储过程的临时表
- N层研习记录01:试图通过Boolean参数控制并发冲突的检查方式(LINQ to SQL)
- 是否会成为问题——Linq to Sql的执行可能无法复用查询计划
- DLINQ(LINQ to SQL)之执行SQL语句的添加、查询、更新和删除