Session

MVC操作数据库(删除数据库信息)

电脑版发表于:2021/3/15 20:28

方法一直接查询SQL语句

//实例表格
oaEntities oaentities = new oaEntities();
//使用SQL语句(不常用)
int count = oaentities.Database.ExecuteSqlCommand("delete from UserInfo where sid=@userid", new SqlParameter("userid", userid));

方法二先查询,在删除

//实例表格
oaEntities oaentities = new oaEntities();
UserInfo userdatatable = oaentities.UserInfo.Where(a => a.sid == (int)usersid).FirstOrDefault();
           if (userdatatable==null)
            {
               return Content("没有该数据");
            }
            //修改状态
            oaentities.UserInfo.Remove(userdatatable);
            //把修改提交到数据库(返回受影响行数)
           int row = oaentities.SaveChanges();

方法三直接删,不用查询,效率最快

            //实例表格
            oaEntities oaentities = new oaEntities();
            
            UserInfo userinfotable = new UserInfo() { sid = (int)usersid };
            //修改的状态
            oaentities.Entry(userinfotable).State = System.Data.EntityState.Deleted;
            //把修改后提交到数据库(返回受影响行数)
            oaentities.SaveChanges();


关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢