.NET F 3.5之后提出的linq to sql 概念,大大的简化了对于数据对象的操作,可以通过简单的语法直接操作数据对象,如List,Linq to sql类 等等。
1.使用Linq to sql 类
2.Linq增删改查数据库
/// <summary> /// 添加 /// </summary> /// <param name="u"></param> public void Add(user u) { using (AccountDataContext db = new AccountDataContext()) { db.user.InsertOnSubmit(u); db.SubmitChanges(); } }
/// <summary> /// 删除 /// </summary> /// <param name="id"></param> public void Delete(int id) { using (AccountDataContext db = new AccountDataContext()) { user channel = db.user.Single(c => c.uid == id); db.user.DeleteOnSubmit(channel); db.SubmitChanges(); } }
/// <summary> /// 修改 /// </summary> /// <param name="id"></param> public void Update(int id) { using (AccountDataContext db = new AccountDataContext()) { user u = db.user.Single(c => c.uid == id); u.name = "修改后的name"; db.SubmitChanges(); } }
/// <summary> /// 查询 /// </summary> public List<user> Select() { using (AccountDataContext db = new AccountDataContext()) { IQueryable<user> u = from c in db.user select c; return u.ToList(); } }