zoukankan      html  css  js  c++  java
  • 第三节:执行一些EF的增删改查

    针对两表操作

    一丶增加

     1 #region 05-增加操作
     2         /// <summary>
     3         /// 05-增加操作
     4         /// </summary>
     5         /// <param name="studentInfo">用来接收用户信息(涵盖多条件)</param>
     6         /// <param name="student_Photo">用来接收用户信息(涵盖多条件)</param>
     7         /// <returns></returns>
     8         public ActionResult AddExcute(StudentInfo studentInfo, Student_Photo student_Photo)
     9         {
    10             try
    11             {
    12                 //为数据库条目赋值
    13                 StudentInfo stuInfo = new StudentInfo()
    14                 {
    15                     id = Guid.NewGuid().ToString("N"),
    16                     name = studentInfo.name,
    17                     sex = studentInfo.sex,
    18                     age = studentInfo.age,
    19                     stuAddTime = DateTime.Now,
    20                 };
    21                 Student_Photo stu_Photo = new Student_Photo()
    22                 {
    23                     id = stuInfo.id,
    24                     photoUrl = student_Photo.photoUrl,
    25                     roomNumber = student_Photo.roomNumber,
    26                     addTime = DateTime.Now,
    27                 };
    28                 //进行插入
    29                 oc.BllSession.IStudentInfoBLL.AddNo(stuInfo);
    30                 oc.BllSession.IStudent_PhotoBLL.AddNo(stu_Photo);
    31                 //提交事务
    32                 int result = oc.BllSession.IStudentInfoBLL.SaveChange();
    33                 if (result > 0)
    34                 {
    35                     return Content("ok");
    36                 }
    37                 else
    38                 {
    39                     return Content("error");
    40                 }
    41             }
    42             catch (Exception ex)
    43             {
    44                 Common.Log.LogHelper.Info(ex.Message);
    45                 return Content("");  //只为了凑返回值,没有实际用处
    46             }
    47 
    48         }
    49         #endregion
    View Code

    二丶删除

     1 #region 06-删除操作
     2         /// <summary>
     3         /// 06-删除操作
     4         /// </summary>
     5         /// <param name="idsStr">id字符串</param>
     6         /// <returns></returns>
     7         public ActionResult DelExcute(string idsStr)
     8         {
     9             try
    10             {
    11                 //进行字符串拆分
    12                 string[] str = idsStr.Split(new char[] { ',' });
    13                 foreach (var item in str)
    14                 {   //根据id查找对应行
    15                     var stu_Info = oc.BllSession.IStudentInfoBLL.Entities.Where(a => a.id == item).FirstOrDefault();
    16                     var stu_Img = oc.BllSession.IStudent_PhotoBLL.Entities.Where(a => a.id == item).FirstOrDefault();
    17                     //进行删除
    18                     oc.BllSession.IStudentInfoBLL.DelNo(stu_Info);
    19                     oc.BllSession.IStudent_PhotoBLL.DelNo(stu_Img);
    20                 }
    21                 //提交事务
    22                 int result = oc.BllSession.IStudentInfoBLL.SaveChange();
    23                 if (result > 0)
    24                 {
    25                     return Content("ok");
    26                 }
    27                 else
    28                 {
    29                     return Content("error");
    30                 }
    31             }
    32             catch (Exception ex)
    33             {
    34                 Common.Log.LogHelper.Info(ex.Message);
    35                 return Content("");  //只为了凑返回值,没有实际用处
    36             }
    37         }
    38         #endregion
    View Code

    三丶修改

     1 #region 07-修改操作
     2         /// <summary>
     3         /// 07-修改操作
     4         /// </summary>
     5         /// <param name="studentInfo">学生表查询信息</param>
     6         /// <param name="student_Photo">学生图片信息</param>
     7         /// <returns></returns>
     8         public ActionResult UpdateExcute(StudentInfo studentInfo, Student_Photo student_Photo)
     9         {
    10             try
    11             {
    12                 //根据id号查询
    13                 var stu_Info = oc.BllSession.IStudentInfoBLL.Entities.Where(a => a.id == studentInfo.id).FirstOrDefault();
    14                 var stu_Img = oc.BllSession.IStudent_PhotoBLL.Entities.Where(a => a.id == studentInfo.id).FirstOrDefault();
    15                 //为学生信息赋值
    16                 stu_Info.name = studentInfo.name;
    17                 stu_Info.sex = studentInfo.sex;
    18                 stu_Info.age = studentInfo.age;
    19                 stu_Info.stuAddTime = DateTime.Now;
    20 
    21                 stu_Img.photoUrl = student_Photo.photoUrl;
    22                 stu_Img.roomNumber = student_Photo.roomNumber;
    23                 stu_Img.addTime = DateTime.Now;
    24                 //进行修改
    25                 oc.BllSession.IStudentInfoBLL.ModifyNo(stu_Info);
    26                 oc.BllSession.IStudent_PhotoBLL.ModifyNo(stu_Img);
    27                 //提交事务
    28                 int result = oc.BllSession.IStudentInfoBLL.SaveChange();
    29                 if (result > 0)
    30                 {
    31                     return Content("ok");
    32                 }
    33                 else
    34                 {
    35                     return Content("error");
    36                 }
    37             }
    38             catch (Exception ex)
    39             {
    40                 Common.Log.LogHelper.Info(ex.Message);
    41                 return Content("");  //只为了凑返回值,没有实际用处
    42             }
    43         }
    44         #endregion
    View Code

    四丶查询

     1 #region 04-多条件查询操作
     2         /// <summary>
     3         /// 04-多条件查询操作
     4         /// </summary>
     5         /// <param name="studentInfo">用来接收用户信息(涵盖多条件)</param>
     6         /// <param name="student_Photo">用来接收用户信息(涵盖多条件)</param>
     7         /// <param name="rows">行数</param>
     8         /// <param name="page">页码</param>
     9         /// <param name="dateStart">起始时间</param>
    10         /// <param name="dateEnd">结束时间</param>
    11         /// <returns></returns>
    12         public ActionResult StuList(StudentInfo studentInfo, Student_Photo student_Photo, int rows, int page, string dateStart, string dateEnd)
    13         {
    14             try
    15             {
    16                 //数据源
    17                 var stu_Info = oc.BllSession.IStudentInfoBLL.Entities;
    18                 var stu_Img = oc.BllSession.IStudent_PhotoBLL.Entities;
    19 
    20                 //下面开始进行过滤查询
    21                 //1.根据学生姓名查询
    22                 if (!String.IsNullOrEmpty(studentInfo.name))
    23                 {
    24                     stu_Info = stu_Info.Where(a => a.name.Contains(studentInfo.name));
    25                 }
    26                 //2.根据学生性别查询
    27                 if (!String.IsNullOrEmpty(studentInfo.sex))
    28                 {
    29                     stu_Info = stu_Info.Where(a => a.sex == studentInfo.sex);
    30                 }
    31                 //3.对时间进行排序
    32                 if (!String.IsNullOrEmpty(dateStart))
    33                 {
    34                     DateTime dateS = Convert.ToDateTime(dateStart);//开始时间
    35                     if (dateS != DateTime.MinValue)
    36                     {
    37                         stu_Info = stu_Info.Where(a => a.stuAddTime > dateS);
    38                     }
    39                 }
    40                 if (!String.IsNullOrEmpty(dateEnd))
    41                 {
    42                     DateTime dateE = Convert.ToDateTime(dateEnd);//开始时间
    43                     if (dateE != DateTime.MinValue)
    44                     {
    45                         stu_Info = stu_Info.Where(a => a.stuAddTime < dateE);
    46                     }
    47                 }
    48                 //4.根据学生宿舍号进行查询
    49                 if (!String.IsNullOrEmpty(student_Photo.roomNumber))
    50                 {
    51                     stu_Img = stu_Img.Where(a => a.roomNumber.Contains(student_Photo.roomNumber));
    52                 }
    53                 //连接查询
    54                 var StuList = (from a in stu_Info.ToList()
    55                                join b in stu_Img.ToList()
    56                                on a.id equals b.id
    57                                select new StuInfoAndImg
    58                                {
    59                                    id = a.id,
    60                                    name = a.name,
    61                                    sex = a.sex,
    62                                    age = a.age,
    63                                    stuAddTime = a.stuAddTime,
    64                                    delFlag = a.delFlag,
    65                                    pId = b.pId,
    66                                    photoUrl = b.photoUrl,
    67                                    roomNumber = b.roomNumber,
    68                                    addTime = b.addTime,
    69                                    delFlag1 = b.delFlag,
    70                                }).OrderByDescending(u => u.stuAddTime);
    71 
    72                 var json = new
    73                 {
    74                     total = StuList.Count(),
    75                     rows = StuList.Skip(rows * (page - 1)).Take(rows).ToList(),
    76                 };
    77                 return Json(json);
    78             }
    79             catch (Exception ex)
    80             {
    81                 Common.Log.LogHelper.Info(ex.Message);
    82                 return Content("");  //只为了凑返回值,没有实际用处
    83             }
    84         }
    85         #endregion
    View Code

    五丶附加

     1 #region 08-上传图片
     2         /// <summary>
     3         /// 08-上传图片
     4         /// </summary>
     5         /// <returns></returns>
     6         public ActionResult Upload()
     7         {
     8             try
     9             {
    10                 if (Request.Files.Count == 0)
    11                 {
    12                     return PackagingAjaxmsg(new AjaxMsgModel { BackUrl = null, Data = null, Msg = "未找到要上传的图片", Statu = AjaxStatu.err });
    13                 }
    14                 else
    15                 {
    16                     //得到上传的图片
    17                     var file = Request.Files[0];
    18                     //要保存的文件路径
    19                     var path = Path.Combine(Server.MapPath(Request.ApplicationPath), "Upload", "Image");
    20                     if (!Directory.Exists(path))
    21                     {
    22                         Directory.CreateDirectory(path);
    23                     }
    24                     //要保存的文件名称
    25                     string fileName = string.Format("{0}_{1}{2}", oc.CurrentUser.uId, DateTime.Now.ToString("yyyyMMddHHmmss"), Path.GetExtension(file.FileName));
    26                     //保存文件到指定的目录
    27                     file.SaveAs(Path.Combine(path, fileName));
    28                     //上传之后图片的相对路径
    29                     string relativePath = Path.Combine(Request.ApplicationPath, "Upload", "Image", fileName);
    30 
    31                     return PackagingAjaxmsg(new AjaxMsgModel { BackUrl = relativePath, Data = null, Msg = "上传成功", Statu = AjaxStatu.ok });
    32                 }
    33             }
    34             catch (Exception ex)
    35             {
    36                 Common.Log.LogHelper.Info(ex.Message);
    37                 return PackagingAjaxmsg(new AjaxMsgModel { BackUrl = null, Data = null, Msg = "图片上传失败", Statu = AjaxStatu.err });
    38             }
    39         }
    40         #endregion
    View Code
    $("#j_btn1").uploadify({
                buttonText: '上传图片',
                height: 20,
                 120,
                swf: '/Content/uploadify/uploadify.swf',
                uploader: '/Test_Areas/Test/Upload',//通过后台的程序把文件上传到服务器
                multi: false,//是否允许同时选择多个文件
                fileSizeLimit: '8MB',//文件大小
                fileTypeExts: '*.gif;*.png;*.jpg;*jpeg',//可选文件的扩展名
                formData: {
                    'folder': '/Upload', 'ASPSESSID': ASPSESSID, 'AUTHID': auth//测试
                },
    
                onUploadSuccess: function (file, data, response) {
                    var jsonData = $.parseJSON(data);
                    $.procAjaxMsg(jsonData, function () {
                        $.alertMsg(jsonData.Msg, '操作提示', function () {
    
                            $("#j_editForm img").attr("src", jsonData.BackUrl);
                            $("#j_ImgUrl").val(jsonData.BackUrl);
                        });
                    }, function () {
                        $.alertMsg(jsonData.Msg, '操作提示', null);
                    });
                },
                onUploadError: function (file, errorCode, errorMsg, errorString) {
                    $.alertMsg('文件 ' + file.name + ' 上传失败: ' + errorString, '上传失败', null);
                },
                onSelectError: function (file, errorCode, errorMsg, errorString) {
                    $.alertMsg('文件 ' + file.name + ' 不能被上传: ' + errorString, '选择失效', null);
                }
            })
    

      

    作者:chenze
    出处:https://www.cnblogs.com/chenze-Index/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    树莓派学习笔记(三)——远程调试树莓派程序(Pycharm实现)
    树莓派学习笔记(一)——系统安装与远程显示
    记 laravel 排除CSRF验证
    thinkPHP5 生成微信小程序二维码 保存在本地
    微信小程序 rich-text 富文本中图片自适应
    Laravel 中自定义 手机号和身份证号验证
    laravel Excel 导入
    微信小程序之页面跳转(tabbar跳转及页面内跳转)
    关于MySQL事务和存储引擎常见FAQ
    微信小程序点击保存图片到本地相册——踩坑
  • 原文地址:https://www.cnblogs.com/chenze-Index/p/9272726.html
Copyright © 2011-2022 走看看