码迷,mamicode.com
首页 > Web开发 > 详细

[ADO.NET 实体数据模型][增删该查]

时间:2014-11-17 19:10:42      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   sp   数据   div   on   

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

标签:style   blog   io   color   os   sp   数据   div   on   

原文地址:http://www.cnblogs.com/me-code/p/4104128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!