强类型DataContext
NorthwindDataContext.cs
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data;
namespace DannyWeb
{
public class NorthwindDataContext : DataContext
{
public Table<Customer> Customers;
public NorthwindDataContext(IDbConnection connection) : base(connection) { }
public NorthwindDataContext(string connection) : base(connection) { }
}
}
[/code]
显示页
Default2.aspx
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DannyWeb;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
NorthwindDataContext ctx = new NorthwindDataContext("server=.;database=Northwind;uid=sa;pwd=");
GridView1.DataSource = from c in ctx.Customers
where c.CustomerID.StartsWith("A")
select new { 顾?客¨ªID = c.CustomerID, 顾?客¨ª名? = c.Name, 城?市ºD = c.City };
GridView1.DataBind();
}
}
[/code]
显示结果:
2011-6-2 13:18 danny
显示日志:
Default2.aspx.cs
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DannyWeb;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
NorthwindDataContext ctx = new NorthwindDataContext("server=.;database=Northwind;uid=sa;pwd=");
StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);
ctx.Log = sw;
GridView1.DataSource = from c in ctx.Customers
where c.CustomerID.StartsWith("A")
select new { 顾?客¨ªID = c.CustomerID, 顾?客¨ª名? = c.Name, 城?市ºD = c.City };
GridView1.DataBind();
sw.Close();
}
}
[/code]
日志
log.txt
[code]
SELECT [t0].[CustomerID] AS [顾客ID], [t0].[ContactName] AS [顾客名], [t0].[City] AS [城市]
FROM [Customers] AS [t0]
WHERE [t0].[CustomerID] LIKE @p0
-- @p0: Input NVarChar (Size = 2; Prec = 0; Scale = 0) [A%]
-- Context: SqlProvider(Sql2000) Model: AttributedMetaModel Build: 4.0.30319.1
[/code]
2011-6-2 13:24 danny
NorthwindDataContext.cs
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data;
namespace DannyWeb
{
public class NorthwindDataContext : DataContext
{
public Table<Customer> Customers;
public NorthwindDataContext(IDbConnection connection) : base(connection) { }
public NorthwindDataContext(string connection) : base(connection) { }
}
}
[/code]
显示页
Default2.aspx
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DannyWeb;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
NorthwindDataContext ctx = new NorthwindDataContext("server=.;database=Northwind;uid=sa;pwd=");
GridView1.DataSource = from c in ctx.Customers
where c.CustomerID.StartsWith("A")
select new { 顾?客¨ªID = c.CustomerID, 顾?客¨ª名? = c.Name, 城?市ºD = c.City };
GridView1.DataBind();
}
}
[/code]
显示结果:
2011-6-2 13:18 danny
显示日志:
Default2.aspx.cs
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DannyWeb;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
NorthwindDataContext ctx = new NorthwindDataContext("server=.;database=Northwind;uid=sa;pwd=");
StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);
ctx.Log = sw;
GridView1.DataSource = from c in ctx.Customers
where c.CustomerID.StartsWith("A")
select new { 顾?客¨ªID = c.CustomerID, 顾?客¨ª名? = c.Name, 城?市ºD = c.City };
GridView1.DataBind();
sw.Close();
}
}
[/code]
日志
log.txt
[code]
SELECT [t0].[CustomerID] AS [顾客ID], [t0].[ContactName] AS [顾客名], [t0].[City] AS [城市]
FROM [Customers] AS [t0]
WHERE [t0].[CustomerID] LIKE @p0
-- @p0: Input NVarChar (Size = 2; Prec = 0; Scale = 0) [A%]
-- Context: SqlProvider(Sql2000) Model: AttributedMetaModel Build: 4.0.30319.1
[/code]
2011-6-2 13:24 danny