首页 > 代码库 > [ADO.NET 实体数据模型][增删该查]
[ADO.NET 实体数据模型][增删该查]
1. 新增
public bool DataAdd(USER model) { bool isSuccess = false; try { using (DataBaseEntities db = new DataBaseEntities()) { db.USER.AddObject(model); isSuccess = db.SaveChanges() > 0; } } catch (Exception ex) { Console.WriteLine("异常信息:"+ex.Message); } return isSuccess; }
2. 删除
public bool DataDelete(USER model) { bool isSuccess = false; try { using (DataBaseEntities db = new DataBaseEntities()) { db.USER.DeleteObject(model); isSuccess = db.SaveChanges() > 0; } } catch (Exception ex) { Console.WriteLine("异常信息:" + ex.Message); } return isSuccess; }
3. 修改
public bool DataEditor(USER model) { bool isSuccess = false; try { using (DataBaseEntities db = new DataBaseEntities()) {
//只有在 DataBaseEntities 对象没有被释放时,才能实时更新对应的数据 USER user = db.USER.First(p => p.ID == model.ID); user.NAME = model.NAME; isSuccess = db.SaveChanges() > 0; } } catch (Exception ex) { Console.WriteLine("异常信息:" + ex.Message); } return isSuccess; }
4. 查询
public List<USER> DataSelect() { List<USER> list = new List<USER>(); try { using (DataBaseEntities db = new DataBaseEntities()) { list = db.USER.ToList(); } } catch (Exception ex) { Console.WriteLine("异常信息:" + ex.Message); } return list; }
[ADO.NET 实体数据模型][增删该查]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。