第三节:执行一些EF的增删改查
针对两表操作
一丶增加
#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);
}
})
第三节:执行一些EF的增删改查的更多相关文章
- easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)
easyui datagrid 禁止选中行 没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...
- EF实现增删改查
从来没想到过能在这个上面翻车,感慨自学没有培训来得系统啊,废话不多说 ORM:对象关系映射(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一 ...
- ASP.NET从零开始学习EF的增删改查
ASP.NET从零开始学习EF的增删改查 最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...
- [.NET源码] EF的增删改查
EF的增删改查 创建上下文对象:WordBoradEntities db = new WordBoradEntities(); 一.添加: //1.1创建实体对象 User uObj = new Us ...
- http://www.cnblogs.com/nangong/p/db29669e2c6d72fb3d0da947280aa1ce.htm ASP.NET从零开始学习EF的增删改查
http://www.cnblogs.com/nangong/p/db29669e2c6d72fb3d0da947280aa1ce.htmlASP.NET从零开始学习EF的增删改查
- EF CodeFirst增删改查之‘CRUD’
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本篇旨在学习EF增删改查四大操作 上一节讲述了EF ...
- [EF]使用EF简单增删改查
目录 认识EF 添加数据 删除数据 修改数据 查询数据 总结 认识EF ADO.NET Entity Framework 是微软以ADO.NET为基础所发展出来的对象关系对伊(O/R Mapping) ...
- ASP.NET MVC学习---(三)EF简单增删改查
那么现在我们已经大概从本质上了解了ef 巴拉巴拉说了一大堆之后 总算要进入ef的正题了 总在口头说也太不行了是吧~ 没错,现在要用ef进行一些实际的操作 做什么呢? 就做一个入门级的增删改查操作吧 废 ...
- MVC学习-用EF做增删改查
在做增删改查先,先介绍几个知识点: 1.代理类 在将对象方法EF数据上下文时,EF会为该对象封装 一个代理类对象, 同时为该对象的每一个属性添加一个标志:unchanged, 当对该对象某个属性进行操 ...
随机推荐
- 使用 Code Map 理解复杂代码1 ——Visual Studio2012
第一次知道code map是在Visual Studio Ultimate 2012自带的解说上面,当时认为十分好奇,所以查了查.结果一查就是好几天.原来Visual Studio Ultimate ...
- openwrt-安装-驱动-应用-lcd2004a实验
1. 板子f403tech的RT5350的板子和 (1)openWRT系统的定义和特点 OpenWrt是一个高度模块化.高度自己主动化的嵌入式Linux系统.拥有强大的网络组件.经常被 ...
- @Override用在哪儿
帮朋友改一段代码,看到好多红叉都是指向@Override. 是这样,他代码里写了一个接口.方法都用抽象函数声明在接口类里.然后在继承自这个接口的实现类里写详细方法的空壳 ...
- oracle 存储过程调用 执行
oracle 存储过程调用 博客分类: 数据库相关 oracle存储过程 2011年02月11日 星期五 14:47 SQL中调用存储过程语句: call procedure_name(); 调用 ...
- bzoj2705 [SDOI2012]Longge的问题——因数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 一开始自己想了半天... 有了点思路:遍历 n 的因数 k,每个因数要预处理出 gcd ...
- bzoj1509
树的直径 我先开始以为是个图,想想并不知道什么求图的直径的方法,结果是棵树 那么直觉告诉我们是在直径上面,实际上就是直径+min(i->u,i->v),扫一遍就行了 #include< ...
- bzoj1050
最小生成树 其实这道题是最小生成树的变种,我们发现答案不一定在最小/最大生成树上,最短路算法也不可行,因为我们我们并不是希望最小值尽量的大,最大值尽量的小,这样不一定是最优的,那么我们枚举最小的边,然 ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居【切比雪夫距离+并查集+multiset】
参考:http://hzwer.com/4361.html 坐标开long long,inf开大点 先曼哈顿转切比雪夫(x+y,x-y),距离就变成了max(x',y'): 先按x排序,维护两个指针, ...
- 1642: [Usaco2007 Nov]Milking Time 挤奶时间(dp)
1642: [Usaco2007 Nov]Milking Time 挤奶时间 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 935 Solved: 55 ...
- 使用printf和String.format格式化输出
格式化输出 在哪些情况下使用格式化输出: 异常打印到日志中使用格式化输出有利于排查错误原因: printf格式化 示例: public class PrintfTest { public static ...