当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET中显示Linq To SQL输出的SQL语句

ASP.NET
asp+文件上传增强实例
ASP.NET学习手记:验证用户表单输入
关于Microsoft.NET Beta1与Visual Studio.NET Alpha不兼容
asp+ 操作Cookie 方法大全
asp+ 现在已经被官方正式更名为 asp.net
在ASP+ 中我们如何使用 Class 而不是组件
asp+ 如何跨站抓取 页面
在 ASp+ 中的一些可能会用到的 小函数
列出asp+中所有request 的属性和数值
妙用asp+的global.asax
一个asp+ 版本的 Active Server Explorer
asp+中的session 的使用和原理() 不需要cookie也可以使用session
asp.net 的菜单制作(asp.net 的菜单application)
如何在asp+ 中使用自定义的pagelet
如何使用asp+ 动态创建页面元素
用DataGrid分页
给上次的DataGrid分页增加些功能!
asp+ 利用数据绑定来处理XML文件
两种没有使用绑定的 数据显示
asp+中的hash表操作

ASP.NET中显示Linq To SQL输出的SQL语句


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 199 ::
收藏到网摘: n/a

最近在使用Linq To SQL的时候,为了了解不同Linq语句对性能造成的不同影响,需要获得Linq To SQL生成的SQL语句。

如果是在桌面程序中,只需要

_context.Log = Console.Out;

即可在控制台输出SQL语句。可是在ASP.NET中又该怎么办呢?

这时我想起了StringWriter。用它就可以代替Console.Out帮我们接收输出的日志,保存在一个StringBuilder里。

于是构造一个辅助类:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.IO;

using System.Text;


namespace Clowwindy.Models

{

public static class LogHelper

{

public static StringBuilder Log = new StringBuilder();

public static TextWriter In = new StringWriter(Log);

public static string GetAllLog()

{

In.Flush();

return Log.ToString();

}

public static void Clean()

{

Log = new StringBuilder();

In = new StringWriter(Log);

}

}

}


再添加一个页面log.aspx,用来显示日志:

onclick="btn_Clean_Click"/>

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Clowwindy.Models;


namespace Clowwindy

{

public partial class Log : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (Request.UserHostAddress != "127.0.0.1")

{

Response.End();

return;

}

Literal1.Text = LogHelper.GetAllLog().Replace("\n","\n
");

}


protected void btn_Clean_Click(object sender, EventArgs e)

{

LogHelper.Clean();

Literal1.Text = null;

}

}

}


最后在所有new DataContext的地方

加上_context.Log = LogHelper.In:

 

public Repository()

{

_context = new TDataContext();

_context.Log = LogHelper.In;

}

打开log.aspx,即可看到之前执行的SQL语句。