[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;
        }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。