Microsoft Study Bible

December 17, 2009

Linq statement in Asp.net

Filed under: Developer tools and applications — Tags: , , , , — Jackson @ 4:58 am

If it is in desktop, you just need to

_context.Log = Console.Out;

You just input SQL statement in the console .So, what about in Asp.net?

I want to try to use StringWriter. We can use it to replace Console.Out to help us receive the output log, and save the log in StringBuider.

So, construct an auxiliary class

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);

 }

 }

 }

 Add another page log.aspx to display the log

 <%@ Page Language=”C#” AutoEventWireup=”true”

 CodeBehind=”Log.aspx.cs” Inherits=”Clowwindy.Log” %>

 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

 http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

 <html xmlns=”http://www.w3.org/1999/xhtml” >

 <head runat=”server”>

 <title>SQL Log</title>

 </head>

 <body>

 <form id=”form1″ runat=”server”>

 <asp:Button ID=”btn_Clean” runat=”server” Text=”Clear out”

 onclick=”btn_Clean_Click”/>

 <div>

 <asp:Literal ID=”Literal1″ runat=”server”></asp:Literal>

 </div>

 </form>

 </body>

 </html>

 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<br/>”);

 }

 protected void btn_Clean_Click(object sender, EventArgs e)

 {

 LogHelper.Clean();

 Literal1.Text = null;

 }

 }

 }

Finally, in the places where new DataContext is, Add _context.Log = LogHelper.In:

public Repository()

 {

 _context = new TDataContext();

 _context.Log = LogHelper.In;

 }

Open log.aspx, you will see the previous SQL statements.

Powered by WordPress

Close
E-mail It