`

lambda表达式4

阅读更多

查看命令
Default3.aspx.cs
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();
    }
}


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


2011-6-2 13:46 danny

创建数据库

实体类:
Test.cs
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; }

}



TestContext.cs
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)
    {

    }
}


创建库,
Default5.aspx.cs
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();
    }
}


2011-6-2 21:29 danny

使用DbDataReader数据源

Default6.aspx.cs
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();
    }
}


结果:


2011-6-2 21:37 danny
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics