.ToList();//返回一个集合,包含查到的所有值;
.First();//返回查到的第一条数据,如果查不到会报错;
.FirstOrDefault();返回查到的第一条数据,差不到返回一个null;
db.Users.InsertOnSubmit();//添加
db.DeleteOrSubmit();//删除
db.SubmitChanges();//提交修改,增删改都需要提交;
//修改取到对象之后直接赋值,提交;
//基础查,把内容放到泛型集合 using(DBDataContext db = new DBDataContext()) { List<Users> ulist = db.Users.ToList(); } //模糊差,contains包含 string a = "123"; using(DBDataContext db = new DBDataContext()) { List<Users> ulist = db.Users.where(r => r.Ids.Contains(a))ToList(); } //精确查, string a = "123"; using(DBDataContext db = new DBDataContext()) { List<Users> ulist = db.Users.where(r => r.Ids==a)ToList(); } //添加 Users u = new Users(); u.ids = "1"; u.UserName = "2"; u.password = "3"; using(DBdataContext db = new DBDataTaxt()) { try { db.Users.InsertOnSubmit(u); db.SubmitChanges(); MessageBox.Show("成功"); } catch { MessageBox.Show("失败"); } } //修改 string a = "1"; using(DBdataContext db = new DBDataTaxt()) { try { Users u = db.Usres.where(r => r.ids == a).FirstOrDefault(); u.ids = "3"; u.UserName = "2"; u.password = "1"; db.SubmitChanges(); MessageBox.Show("成功"); } catch { MessageBox.Show("失败"); } } //删除 string a = "1"; using(DBdataContext db = new DBDataTaxt()) { List<Users> ulist = db.Users.where(r => r.Ids == a).ToList(); if(ulist.Count > 0) { db.DeleteOnSubmit(ulist[0]); db.SubmitChanges(); } }