lambda表达式4

查看命令
Default3.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;
using System.Data.Common;

public partial class Default3 : 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("log3.txt"), true);
ctx.Log = sw;
var select=from c in ctx.Customers where c.CustomerID.StartsWith("A")
select new { 顾?客¨ªID = c.CustomerID, 顾?客¨ª名? = c.Name, 城?市ºD = c.City };
DbCommand cmd = ctx.GetCommand(select);
Response.Write(cmd.CommandText + "<br/>");
foreach (DbParameter parm in cmd.Parameters)
Response.Write(string.Format("参?数ºy:{0},参?数ºy值¦Ì:{1}<br/>", parm.ParameterName, parm.Value));
Customer customer = ctx.Customers.First();
customer.Name = "zhuye";
IList<object> queryText = ctx.GetChangeSet().Updates;
Response.Write(((Customer)queryText[0]).Name);

sw.Close();
}
}

[/code]
显示结果:
[code]
SELECT [t0].[CustomerID] AS [顾客ID], [t0].[ContactName] AS [顾客名], [t0].[City] AS [城市] FROM [Customers] AS [t0] WHERE [t0].[CustomerID] LIKE @p0
参数:@p0,参数值:A%
zhuye
[/code]

2011-6-2 13:46 danny

创建数据库

实体类:
Test.cs
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;

[Table(Name = "test")]
public class Test
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID { get; set; }

[Column(DbType = "varchar(20)")]
public string Name { get; set; }

}
[/code]


TestContext.cs
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///TestContext 的Ì?摘a要°a说¦Ì明¡Â
/// </summary>
public class TestContext : DataContext
{
public Table<Test> test;
public TestContext(string connection)
: base(connection)
{

}
}
[/code]

创建库,
Default5.aspx.cs
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestContext ctx = new TestContext("Server=.;database=testdb;uid=sa;pwd=");
ctx.CreateDatabase();
}
}
[/code]

2011-6-2 21:29 danny

使用DbDataReader数据源

Default6.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 System.Data.SqlClient;
using System.Data.Linq;
using DannyWeb;

public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var conn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=");
var ctx = new DataContext(conn);
var cmd = new SqlCommand("select * from customers where customerID like 'A%'", conn);
conn.Open();

var reader = cmd.ExecuteReader();
GridView1.DataSource = ctx.Translate<Customer>(reader);
GridView1.DataBind();
conn.Close();
}
}
[/code]

结果:


2011-6-2 21:37 danny
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值