首页 > 代码库 > [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 实体数据模型][增删该查]