与数据库关联实例

 

using System.Data.Linq.Mapping;//定义实体要

using System.Data.Linq;

using System.Data.SqlClient;

using System.IO; //StreamWriter需要

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

{

    [Table(Name = "Customers")]               //定义实体

    public class Customer

    {

        [Column(IsPrimaryKey = true)]

        public string CustomerID { get; set; }

        [Column(Name = "ContactName")]

        public string Name { get; set; }

        [Column]

        public string City { get; set; }

    }

    public partial class theDataContext : DataContext

    {

        public Table<Customer> Customers;

        public theDataContext(IDbConnection connection) : base(connection) //调用直接父类构造函数

        { }

        public theDataContext(string connection) : base(connection) //调用直接父类构造函数

        { }

    }

    protected void Page_Load(object sender, EventArgs e)

    {

        theDataContext ctx = new theDataContext("Data Source=CHINA-180073D63""SQLEXPRESS;Initial Catalog=db_Sell;Persist Security Info=True;User ID=sa");

        StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true); // Append

        ctx.Log = sw;

        GridView1.DataSource = from c in ctx.Customers where c.CustomerID.StartsWith("A") select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City };

        GridView1.DataBind();

        sw.Close();

    }

}

posted @ 2009-04-28 11:08  失落的狼崽  阅读(262)  评论(0编辑  收藏  举报