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操作 插入( ...
随机推荐
- angularjs指令中的compile与link函数详解(转)
http://www.jb51.net/article/58229.htm 通常大家在使用ng中的指令的时候,用的链接函数最多的是link属性,下面这篇文章将告诉大家complie,pre-link, ...
- 对于随机变量的标准差standard deviation、样本标准差sample standard deviation、标准误差standard error的解释
参考:http://blog.csdn.net/ysuncn/article/details/1749729
- IPicture、BITMAP、HBITMAP和CBitmap的关系
1.有关IPicture加载图片后直接Render到内存DC的问题(HBITMAP转换IPicture)Picture的方法get_Handle可以直接得到图片的句柄 IPicture *pIPict ...
- OutputCache缓存各参数的说明
Duration 缓存时间,以秒为单位,这个除非你的Location=None,可以不添加此属性,其余时候都是必须的. Location Location当被设置为None时,其余的任何设置将不起作用 ...
- Android中的音频播放(MediaPlayer和SoundPool)
Android中音频和视频的播放我们最先想到的就是MediaPlayer类了,该类提供了播放.暂停.停止.和重复播放等方法.该类位于android.media包下,详见API文档.其实除了这个类还有一 ...
- 《Python核心编程》 第十章 错误和异常
10–1. 引发异常. 以下的哪个因素会在程序执行时引发异常? 注意这里我们问的并不是异常的原因. a) 用户 b) 解释器 c) 程序 d) 以上所有 e) 只有 b) 和 c) f) 只有 a) ...
- VS2010生成Qt程序图标修改方法
转自:http://blog.csdn.net/simeone18/article/details/7344547 1.准备ico文件,nuistcard.ico 2.在nuistcard工程目录,建 ...
- duilib corner属性的贴图技巧——让图片自动贴到控件的的某一边或者一角并自适应控件的大小
转载请说明原出处,谢谢~~ Duilib给控件贴图功能可以附带多个属性,各个属性的配合可以达到许多效果.以下是duilib支持的所有贴图属性: 贴图描述: Duilib的表现力丰富很大程度上得益于贴图 ...
- C++实现网格水印之调试笔记(三)—— 初有结果
错误: error C2338: THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD 这种错误 ...
- Linux性能监控之Memory篇
首先说说虚拟内存和物理内存: 虚拟内存就是采用硬盘来对物理内存进行扩展,将暂时不用的内存页写到硬盘上而腾出更多的物理内存让有需要的进程来用.当这些内存页需要用的时候在从硬盘读回内存.这一切对于用户来说 ...