linq to sql 三层架构中使用CRUD操作
/// <summary>
/// 数据层
/// </summary>
public partial class GasBottles : IGasBottles
{
#region IGasBottles 成员 public Model.GasBottles GetModel(int gasBottlesID)
{
var db = DbContext.LGSCMSDataContext;
try
{
var gs = db.GasBottles.FirstOrDefault(s => s.ID == gasBottlesID);
if (gs != null)
{
Model.GasBottles gasBottlesInfo = gs.ConvertToEntity<Model.GasBottles>();
return gasBottlesInfo;
}
}
catch (Exception ex)
{
throw ex;
} return null;
} public bool Add(Model.GasBottles gasBottles)
{
bool flag = false;
try
{
var db = DbContext.LGSCMSDataContext;
DataLinqEntity.GasBottles gs = gasBottles.ConvertToEntity<DataLinqEntity.GasBottles>();
db.GasBottles.InsertOnSubmit(gs);
db.SubmitChanges();
flag = true;
}
catch (Exception ex)
{
flag = false;
throw ex;
}
return flag;
} public bool Update(Model.GasBottles gasBottles)
{
bool flag = false;
var changedData = gasBottles.ConvertToEntity<DataLinqEntity.GasBottles>();
var db = DbContext.LGSCMSDataContext;
try
{
var updateTarget = db.GasBottles.SingleOrDefault(i => i.ID == gasBottles.ID);
//CopyProperties(ref updateTarget, changedData);
changedData.GetType().GetProperties()
.Where(p => p.GetCustomAttributes(typeof(ColumnAttribute), false).Any()).ToList()
.ForEach(p => p.SetValue(updateTarget, p.GetValue(changedData, null), null));
db.SubmitChanges();
flag = true;
}
catch (Exception ex)
{
flag = false;
throw ex;
}
return flag;
} public bool Delete(int gasBottlesID)
{
bool flag = false;
var db = DbContext.LGSCMSDataContext;
try
{
var gs = db.GasBottles.FirstOrDefault(s => s.ID == gasBottlesID);
if (gs != null)
{
gs.deleteflag = ;
db.SubmitChanges();
}
}
catch (Exception ex)
{
flag = false;
throw ex;
} return flag;
} /// <summary>
/// 新增或更新
/// </summary>
/// <param name="gasBottles"></param>
/// <param name="gasBottlesID"></param>
/// <returns></returns>
public bool Save(Model.GasBottles gasBottles, out int gasBottlesID)
{
bool flag = false;
var changedData = gasBottles.ConvertToEntity<DataLinqEntity.GasBottles>();
var db = DbContext.LGSCMSDataContext;
try
{
//=>新增
var updateTarget = db.GasBottles.SingleOrDefault(i => i.ID == gasBottles.ID);
if (updateTarget == null)
{
db.GasBottles.InsertOnSubmit(changedData);
db.SubmitChanges();
gasBottlesID = changedData.ID.ToInt();
flag = true;
}
//=>修改
else
{
//CopyProperties(ref updateTarget, changedData);
changedData.GetType().GetProperties()
.Where(p => p.GetCustomAttributes(typeof(ColumnAttribute), false).Any()).ToList()
.ForEach(p => p.SetValue(updateTarget, p.GetValue(changedData, null), null));
db.SubmitChanges();
gasBottlesID = updateTarget.ID.ToInt();
flag = true;
}
}
catch (Exception ex)
{
flag = false;
throw ex;
}
return flag;
} #endregion
}
private void CopyProperties<T>(ref T Target, T Source)
{
foreach (PropertyInfo PI in Target.GetType().GetProperties())
{
if (PI.CanWrite && PI.CanRead)
{
PI.SetValue(Target, PI.GetValue(Source, null), null);
}
}
}
#endregion
DbContext
public class DbContext
{
/// <summary>
///
/// </summary>
private readonly static string connectionString = SqlHelper.SQLConnString; #region [=>Winfrom方式]
//private static WLMQGasBottlesDataContext _WLMQGasBottlesDataContext;
///// <summary>
/////
///// </summary>
//public static WLMQGasBottlesDataContext WLMQGasBottlesDataContext
//{
// get
// {
// return _WLMQGasBottlesDataContext ?? new WLMQGasBottlesDataContext(connectionString);
// }
//}
#endregion #region [=>Web方式]
public static WLMQGasBottlesDataContext WLMQGasBottlesDataContext
{
get
{
WLMQGasBottlesDataContext context = HttpContext.Current.Items["WLMQGasBottlesDataContext"] as WLMQGasBottlesDataContext;
if (context == null)
{
context = new WLMQGasBottlesDataContext(connectionString);
HttpContext.Current.Items["WLMQGasBottlesDataContext"] = context;
}
return context;
}
} public static LGSCMSDataContext LGSCMSDataContext
{
get
{
LGSCMSDataContext context = HttpContext.Current.Items["LGSCMSDataContext"] as LGSCMSDataContext;
if (context == null)
{
context = new LGSCMSDataContext(connectionString);
HttpContext.Current.Items["LGSCMSDataContext"] = context;
}
return context;
}
}
#endregion
}
linq to sql 三层架构中使用CRUD操作的更多相关文章
- 三层架构中bll层把datatable转换为实体model的理解
看了很多人的项目,很多都是用到三层架构,其中BLL层中有一种将DataTable转换为实体的方法.一直没有明白为啥要这样做,今天特意去搜索了一下,如果没有答案我是准备提问,寻求解答了.还好找到一个相关 ...
- 谈谈三层架构中Model的作用
Model又叫实体类,这个东西,大家可能觉得不好分层.包括我以前在内,是这样理解的:UI<-->Model<-->BLL<-->Model<-->DAL ...
- 为什么三层架构中业务层(service)、持久层(dao)需要使用一个接口?
为什么三层架构中业务层(service).持久层(dao)需要使用一个接口? 如果没有接口那么我们在控制层使用业务层或业务层使用持久层时,必须要学习每个方法,若哪一天后者的方法名改变了则直接影响到前面 ...
- 【转】.NET 三层架构 中 DAL+IDAL+Model+BLL+Web
其实三层架构是一个程序最基本的 在.Net开发中通常是多层开发 比如说 BLL 就是business Logic laywer(业务逻辑层) 他只负责向数据提供者也就是DAL调用数据 然后传递给 客户 ...
- 事务管理在三层架构中应用以及使用ThreadLocal再次重构
本篇将详细讲解如何正确地在实际开发中编写事务处理操作,以及在事务处理的过程中使用ThreadLocal的方法. 在前面两篇博客中已经详细地介绍和学习了DbUtils这个Apache的工具类,那么在本篇 ...
- 关于C#三层架构中的“分页”功能
新手上路,请多指教! 今天将分页功能实现了,要特别感谢坐在前面的何同学的指点,不胜感谢!功能的实现采用了三层架构的方式实现该功能,简述如下: 界面: DAL层有两个方法:“当前所在页”和“总页数” 这 ...
- MVC3+Linq to sql 显示数据库中数据表的数据
1:首先创建asp.net mvc3应用程序 2:创建项目完成后 找到controllers文件鼠标右击选择添加控制器 3 为models文件夹添加一个linq to sql类文件,然后把数据库中的数 ...
- Linq to SQL 语法查询(子查询 & in操作 & join )
var 子查询 = from c in ctx.Customers where (from o in ctx.Ord ...
- LINQ体验(9)——LINQ to SQL语句之Insert/Update/Delete操作
我们继续讲解LINQ to SQL语句,这篇我们来讨论Insert/Update/Delete操作.这个在我们的程序中最为常用了.我们直接看例子. Insert/Update/Delete操作 插入( ...
随机推荐
- hdu 4690 EBCDIC
还有什么好说的呢?打表题= = #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- hdu 4614 Vases and Flowers(线段树:成段更新)
线段树裸题.自己写复杂了,准确说是没想清楚就敲了. 先是建点为已插花之和,其实和未插花是一个道理,可是开始是小绕,后来滚雪球了,跪了. 重新建图,分解询问1为:找出真正插画的开始点和终止点,做成段更新 ...
- IPicture、BITMAP、HBITMAP和CBitmap的关系
1.有关IPicture加载图片后直接Render到内存DC的问题(HBITMAP转换IPicture)Picture的方法get_Handle可以直接得到图片的句柄 IPicture *pIPict ...
- lnmp脚本
#!/bin/sh echo "欢迎使用 lnmp 脚本 (fanshengshuai@gmail.com) "; echo "增加资源..."; rpm -i ...
- JS 代码编一个倒时器
有时候在生活中,你需要一个JavaScript倒计时时钟,而不是一个末日装置设备.不管你是否有一次约会,销售.促销.或者游戏,你可以受益于使用原生JavaScript构建一个时钟,而不是拿到一个现成的 ...
- 构建属于自己的ORM框架之二--IQueryable的奥秘
上篇文章标题乱起,被吐槽了,这次学乖了. 上篇文章中介绍了如何解析Expression生成对应的SQL语句,以及IQueryable的一些概念,以及我们所搭建的框架的思想等.但还没把它们结合并应用起来 ...
- delete drop truncate
一.相同点 1 truncate.不带where子句的delete.drop都会删除表内的数据2 drop.truncate都是DDL语句(数据定义语言),执行后会自动提交 二.不同点 1trunca ...
- 一行命令实现Android自动关机
前几天晚上失眠,实在睡不着觉,于是想用Nexus7听一听小野丽莎的歌,在安静祥和之中睡去(怎么感觉有点...)但是不能让平板总是这么循环播放吧(屋里吐槽Google Play Music),所以在平板 ...
- (一)使用Blender导出GameMaker支持的模型脚本
源于YOYO论坛帖子:http://gmc.yoyogames.com/index.php?showtopic=603723 既然想做3D,那就先从模型的导入开始,具体的源文件,可以在“(二)使用等高 ...
- 【转载】cocos2d-x教程 Mac系统下搭建Lua的编码环境
原文链接:http://blog.csdn.net/u012945598/article/details/17168831 在使用Lua写脚本的时候大家都会因为没有代码提示导致敲代码的效率有所下降 ...