数据库与MVC增的增删查改之新增

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

开发工具与关键技术:Visual Studio 2015,MVC增

作者: 梁柏源

撰写时间:2019/4/11

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

由于上次分享查询和删除的时候,本人因疏忽把标题写成了新增和删除;这次就把新增给补回来。

同意的拿数据库和MVC做对比,下面是数据库新增的操作:

数据库的新增insert:(假设要把123,4,'长跑','06','跑步' ,1803)

insert into 体育表(运动ID,跑步ID, 专业名称, 运动员编号,班级)

 values(123,4,'长跑','06','跑步' ,1803)

就这样就完了,不过要注意以下几点:

  1. 插入的值的个数和数据类型必须和表结构定义的列的个数和数据类型保持一致,必须一致,不然就白写了。
  2. 插入的值,所有的符号都是英文状态下的
  3. 新增的外键的值,必须在主键表中存在。
  4. 每一次新增只能操作一个表

MVC中的新增例子如下:

public ActionResult Insert (PW_Student pwStudent, PW_User pwUser, HttpPostedFileBase fileStudentImage)

        {

            ReturnJson returnJson = new ReturnJson();

            try

            {

                var studentCount = (from tbStudent in myModels.PW_Student

                   where tbStudent.studentID == pwStudent.studentID ||

                         tbStudent.StudentIDNum == pwStudent.StudentIDNum ||

                         tbStudent.StudentNumber == pwStudent.StudentNumber

                                    select tbStudent).Count();

                if (studentCount == 0)

                {       

                    var userCount = (from tbUser in myModels.PW_User

                                     where tbUser.UserNuber == pwStudent.StudentNumber || tbUser.UniformAuthenticationCode == pwUser.UniformAuthenticationCode

                                     select tbUser).Count();

                    if (userCount == 0)

                    {                 

                        pwUser.UserNuber = pwStudent.StudentNumber;

                        //密码与学号一致,密码要加密

                        pwUser.Password = Common.AESEncryptHelper.Encrypt(pwStudent.StudentNumber);

                        pwUser.ToVoidNo = true;//有效账号

                        myModels.PW_User.Add(pwUser);

                      

                            pwStudent.UserID = userId;//用户ID

                            pwStudent.StudentState = "应届";

                            myModels.PW_Student.Add(pwStudent);

 

                            //三、新增用户角色明细表

                            PW_UserRoleDetail pwUserRoleDetail = new PW_UserRoleDetail();

                            pwUserRoleDetail.UserID = userId;

                            pwUserRoleDetail.UserTypeID = 6; //对应数据库的类型:6:表示学生

                            myModels.PW_UserRoleDetail.Add(pwUserRoleDetail);

                            myModels.SaveChanges();

                            returnJson.State = true;

                            returnJson.Text = "信息保存成功!";

 

                        }

                        else

                        {

                            returnJson.State = false;

                            returnJson.Text = "保存失败!";

                        }

 

                    }

                }

                else

                {

                    returnJson.State = false;

                    returnJson.Text = "该信息已经存在!";

                }

            }

            catch (Exception e)

            {

                Console.WriteLine(e);

                returnJson.State = false;

                returnJson.Text = "数据异常!";

            }

            return Json(returnJson, JsonRequestBehavior.AllowGet);

        }

看本来是这样的下面再加一个上去:

然后点击保存,弹出提示框;

点击确定,新增成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值