tnblog
首页
视频
资源
登录

MVC 查询 修改 删除

4697人阅读 2021/3/16 16:30 总访问:116017 评论:0 收藏:0 手机
分类: MVC
public ActionResult Index(UserInfo user)
        {
            oaEntities1 oAEntities = new oaEntities1();
            //方法一用SQL语句
            List<UserInfo> users = oAEntities.Database.SqlQuery<UserInfo>("select * from UserInfo").ToList();
            //方法二用linq语句如果有多张表那就可以选择
            // List<UserInfo> user2 = (from u in oAEntities.UserInfo select u).ToList();
            //方法三用lamda
            // List<UserInfo> user3 = oAEntities.UserInfo.ToList();
            List<UserInfo> user3 = oAEntities.UserInfo.Where(a => (string.IsNullOrEmpty(user.username) || a.username == user.username) &&
                (string.IsNullOrEmpty(user.Number) || a.Number == user.Number)
                ).ToList();
            ViewBag.user = user3;
            return View(user);
        }
        //查询
        public ActionResult Index1(Class1 user)
        {
            List<string> satta = new List<string>() { "请选择审核状态", "通过", "未通过", "审核中" };
            oaEntities1 oAEntities = new oaEntities1();
            //方法一用SQL语句
            if (user.numbers != null && user.names != null)
            {
                int users = oAEntities.Database.ExecuteSqlCommand("delete UserInfo where  Number='" + user.numbers + "' and " + "username='" + user.names + "'");
            }
            //方法二用linq语句如果有多张表那就可以选择
            // List<UserInfo> user2 = (from u in oAEntities.UserInfo select u).ToList();
            //方法三用lamda
            List<UserInfo> userw = oAEntities.UserInfo.ToList();
            List<UserInfo> user3 = userw.Where(a =>
                {
                    if (user.state == 0)
                    {
                        return (string.IsNullOrEmpty(user.username) || a.username == user.username) && (string.IsNullOrEmpty(user.Number) || a.Number == user.Number);
                    }
                    return (string.IsNullOrEmpty(user.username) || a.username == user.username) && (string.IsNullOrEmpty(user.Number) || a.Number == user.Number) && a.state == satta[user.state];
                }
                ).ToList();
            ViewBag.user = user3;
            ViewBag.user_ = user;
            return View(user);
        }
        //删除
        public ActionResult Index2(string name, string number, int ID)
        {
            oaEntities1 oAEntities = new oaEntities1();
            //方法一用SQL语句删除
            // List< SqlParameter> sq = new  List<SqlParameter>();
            // sq.Add(new SqlParameter("number", number));
            // sq.Add(new SqlParameter("name", name));
            // int users = oAEntities.Database.ExecuteSqlCommand("delete UserInfo where  Number=@number and username=@name",sq.ToArray());
            //方法二先查询再删除
            //UserInfo userInfodelete = oAEntities.UserInfo.Where(a => a.Number == number&&a.username==name).FirstOrDefault();
            //if (userInfodelete == null)
            //{
            //    return Content("没有数据");
            //}
            ////修改状态
            //oAEntities.UserInfo.Remove(userInfodelete);
            ////把修改的状态提交到数据库
            //int count = oAEntities.SaveChanges();
            //删除方法三(效率快,应为不用多查询一次)只能利用带有主键的字段单独或者一起删除
            UserInfo userInfodelete = new UserInfo();
            userInfodelete.Number = number;
            userInfodelete.username = name;
            userInfodelete.id = ID;
            //修改状态将其上下文跟踪
            oAEntities.UserInfo.Attach(userInfodelete);
            //修改状态需先上下文跟踪
            oAEntities.UserInfo.Remove(userInfodelete);
            int count = oAEntities.SaveChanges();
            bool a = false;
            if (count > 0)
            {
                a = true;
            }
            Response.Write(a);
            return RedirectToAction("Index1");
        }
        //修改
        public ActionResult Index3(UserInfo info)
        {
            //0用SQL语句
            //oaEntities1 oAEntities = new oaEntities1();
            // int users = oAEntities.Database.ExecuteSqlCommand(SQL语句);
            //1查询一次再跟新
            //oaEntities1 oAEntities = new oaEntities1();
            //UserInfo userInfupdate = oAEntities.UserInfo.Where(a => a.id == info.id).FirstOrDefault();
            //userInfupdate.id = info.id;
            //userInfupdate.Number = info.Number;
            //userInfupdate.userclass = info.userclass;
            //userInfupdate.username = info.username;
            //userInfupdate.state = info.state;
            
            //2不需要查询直接修改(改变的是这个字段的全部,如果只修改一部分那就不能使用这个不让剩下的值会为空)
            //oaEntities1 oAEntities = new oaEntities1();
            //oAEntities.Entry(info).State = System.Data.EntityState.Modified;
            
            //更改属性
             //Modified :修改数据
             //Added   :添加数据
             //Deleted  :删除数据
             //Detached :对象没有被添加到上下文中
             //Unchanged :对象在上下文中没有被改变,没有任何操作
             
            //3不需要查询直接修改且修改指定字段
            oaEntities1 oAEntities = new oaEntities1();
            var staa = oAEntities.UserInfo.Attach(info);
            var sta = oAEntities.Entry(info);
            sta.Property("id").IsModified = true;
            sta.Property("Number").IsModified = true;
            sta.Property("userclass").IsModified = true;
            sta.Property("username").IsModified = true;
            sta.Property("state").IsModified = true;
            int count= oAEntities.SaveChanges();
            return Json(count);
        }

                            

评价
.
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术