针对两表操作
一丶增加
#region 05-增加操作
/// <summary>
/// 05-增加操作
/// </summary>
/// <param name="studentInfo">用来接收用户信息(涵盖多条件)</param>
/// <param name="student_Photo">用来接收用户信息(涵盖多条件)</param>
/// <returns></returns>
public ActionResult AddExcute(StudentInfo studentInfo, Student_Photo student_Photo)
{
try
{
//为数据库条目赋值
StudentInfo stuInfo = new StudentInfo()
{
id = Guid.NewGuid().ToString("N"),
name = studentInfo.name,
sex = studentInfo.sex,
age = studentInfo.age,
stuAddTime = DateTime.Now,
};
Student_Photo stu_Photo = new Student_Photo()
{
id = stuInfo.id,
photoUrl = student_Photo.photoUrl,
roomNumber = student_Photo.roomNumber,
addTime = DateTime.Now,
};
//进行插入
oc.BllSession.IStudentInfoBLL.AddNo(stuInfo);
oc.BllSession.IStudent_PhotoBLL.AddNo(stu_Photo);
//提交事务
int result = oc.BllSession.IStudentInfoBLL.SaveChange();
if (result > )
{
return Content("ok");
}
else
{
return Content("error");
}
}
catch (Exception ex)
{
Common.Log.LogHelper.Info(ex.Message);
return Content(""); //只为了凑返回值,没有实际用处
} }
#endregion
二丶删除
#region 06-删除操作
/// <summary>
/// 06-删除操作
/// </summary>
/// <param name="idsStr">id字符串</param>
/// <returns></returns>
public ActionResult DelExcute(string idsStr)
{
try
{
//进行字符串拆分
string[] str = idsStr.Split(new char[] { ',' });
foreach (var item in str)
{ //根据id查找对应行
var stu_Info = oc.BllSession.IStudentInfoBLL.Entities.Where(a => a.id == item).FirstOrDefault();
var stu_Img = oc.BllSession.IStudent_PhotoBLL.Entities.Where(a => a.id == item).FirstOrDefault();
//进行删除
oc.BllSession.IStudentInfoBLL.DelNo(stu_Info);
oc.BllSession.IStudent_PhotoBLL.DelNo(stu_Img);
}
//提交事务
int result = oc.BllSession.IStudentInfoBLL.SaveChange();
if (result > )
{
return Content("ok");
}
else
{
return Content("error");
}
}
catch (Exception ex)
{
Common.Log.LogHelper.Info(ex.Message);
return Content(""); //只为了凑返回值,没有实际用处
}
}
#endregion
三丶修改
#region 07-修改操作
/// <summary>
/// 07-修改操作
/// </summary>
/// <param name="studentInfo">学生表查询信息</param>
/// <param name="student_Photo">学生图片信息</param>
/// <returns></returns>
public ActionResult UpdateExcute(StudentInfo studentInfo, Student_Photo student_Photo)
{
try
{
//根据id号查询
var stu_Info = oc.BllSession.IStudentInfoBLL.Entities.Where(a => a.id == studentInfo.id).FirstOrDefault();
var stu_Img = oc.BllSession.IStudent_PhotoBLL.Entities.Where(a => a.id == studentInfo.id).FirstOrDefault();
//为学生信息赋值
stu_Info.name = studentInfo.name;
stu_Info.sex = studentInfo.sex;
stu_Info.age = studentInfo.age;
stu_Info.stuAddTime = DateTime.Now; stu_Img.photoUrl = student_Photo.photoUrl;
stu_Img.roomNumber = student_Photo.roomNumber;
stu_Img.addTime = DateTime.Now;
//进行修改
oc.BllSession.IStudentInfoBLL.ModifyNo(stu_Info);
oc.BllSession.IStudent_PhotoBLL.ModifyNo(stu_Img);
//提交事务
int result = oc.BllSession.IStudentInfoBLL.SaveChange();
if (result > )
{
return Content("ok");
}
else
{
return Content("error");
}
}
catch (Exception ex)
{
Common.Log.LogHelper.Info(ex.Message);
return Content(""); //只为了凑返回值,没有实际用处
}
}
#endregion
四丶查询
#region 04-多条件查询操作
/// <summary>
/// 04-多条件查询操作
/// </summary>
/// <param name="studentInfo">用来接收用户信息(涵盖多条件)</param>
/// <param name="student_Photo">用来接收用户信息(涵盖多条件)</param>
/// <param name="rows">行数</param>
/// <param name="page">页码</param>
/// <param name="dateStart">起始时间</param>
/// <param name="dateEnd">结束时间</param>
/// <returns></returns>
public ActionResult StuList(StudentInfo studentInfo, Student_Photo student_Photo, int rows, int page, string dateStart, string dateEnd)
{
try
{
//数据源
var stu_Info = oc.BllSession.IStudentInfoBLL.Entities;
var stu_Img = oc.BllSession.IStudent_PhotoBLL.Entities; //下面开始进行过滤查询
//1.根据学生姓名查询
if (!String.IsNullOrEmpty(studentInfo.name))
{
stu_Info = stu_Info.Where(a => a.name.Contains(studentInfo.name));
}
//2.根据学生性别查询
if (!String.IsNullOrEmpty(studentInfo.sex))
{
stu_Info = stu_Info.Where(a => a.sex == studentInfo.sex);
}
//3.对时间进行排序
if (!String.IsNullOrEmpty(dateStart))
{
DateTime dateS = Convert.ToDateTime(dateStart);//开始时间
if (dateS != DateTime.MinValue)
{
stu_Info = stu_Info.Where(a => a.stuAddTime > dateS);
}
}
if (!String.IsNullOrEmpty(dateEnd))
{
DateTime dateE = Convert.ToDateTime(dateEnd);//开始时间
if (dateE != DateTime.MinValue)
{
stu_Info = stu_Info.Where(a => a.stuAddTime < dateE);
}
}
//4.根据学生宿舍号进行查询
if (!String.IsNullOrEmpty(student_Photo.roomNumber))
{
stu_Img = stu_Img.Where(a => a.roomNumber.Contains(student_Photo.roomNumber));
}
//连接查询
var StuList = (from a in stu_Info.ToList()
join b in stu_Img.ToList()
on a.id equals b.id
select new StuInfoAndImg
{
id = a.id,
name = a.name,
sex = a.sex,
age = a.age,
stuAddTime = a.stuAddTime,
delFlag = a.delFlag,
pId = b.pId,
photoUrl = b.photoUrl,
roomNumber = b.roomNumber,
addTime = b.addTime,
delFlag1 = b.delFlag,
}).OrderByDescending(u => u.stuAddTime); var json = new
{
total = StuList.Count(),
rows = StuList.Skip(rows * (page - )).Take(rows).ToList(),
};
return Json(json);
}
catch (Exception ex)
{
Common.Log.LogHelper.Info(ex.Message);
return Content(""); //只为了凑返回值,没有实际用处
}
}
#endregion
五丶附加
#region 08-上传图片
/// <summary>
/// 08-上传图片
/// </summary>
/// <returns></returns>
public ActionResult Upload()
{
try
{
if (Request.Files.Count == )
{
return PackagingAjaxmsg(new AjaxMsgModel { BackUrl = null, Data = null, Msg = "未找到要上传的图片", Statu = AjaxStatu.err });
}
else
{
//得到上传的图片
var file = Request.Files[];
//要保存的文件路径
var path = Path.Combine(Server.MapPath(Request.ApplicationPath), "Upload", "Image");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//要保存的文件名称
string fileName = string.Format("{0}_{1}{2}", oc.CurrentUser.uId, DateTime.Now.ToString("yyyyMMddHHmmss"), Path.GetExtension(file.FileName));
//保存文件到指定的目录
file.SaveAs(Path.Combine(path, fileName));
//上传之后图片的相对路径
string relativePath = Path.Combine(Request.ApplicationPath, "Upload", "Image", fileName); return PackagingAjaxmsg(new AjaxMsgModel { BackUrl = relativePath, Data = null, Msg = "上传成功", Statu = AjaxStatu.ok });
}
}
catch (Exception ex)
{
Common.Log.LogHelper.Info(ex.Message);
return PackagingAjaxmsg(new AjaxMsgModel { BackUrl = null, Data = null, Msg = "图片上传失败", Statu = AjaxStatu.err });
}
}
#endregion
$("#j_btn1").uploadify({
buttonText: '上传图片',
height: 20,
width: 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);
}
})