Linq 增删改查
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class WorkshopDAL
{
PlantDataClassesDataContext objPlantDataClassesDataContext = new PlantDataClassesDataContext();
/// <summary>
/// 在数据库中查询满足条件的车间信息
/// </summary>
/// <param name="strWerks"></param>
/// <returns></returns>
public List<WorkShop> Query(string strWerks)
{
//在表tbl_WorkshopInfo中查询出strWerks工厂下的所有车间,构造List<WorkShop>返回
List<WorkShop> lstWorkShop = new List<WorkShop>();
try
{
var workshop = (from m in objPlantDataClassesDataContext.tbl_WorkshopInfo
where m.Workshop_Werks.Equals(strWerks)
select m).ToList();
if (workshop.Count == 0) return null;
foreach (tbl_WorkshopInfo item in workshop)
{
WorkShop objWorkShop = new WorkShop();
objWorkShop.Code = item.Workshop_WorkshopCode;
objWorkShop.Description = item.Workshop_WorkshopDes;
objWorkShop.Id = item.Workshop_Id;
Plant objPlant = new Plant();
objPlant.Werks = item.Workshop_Werks;
objWorkShop.Plant = objPlant;
objWorkShop.RecordChangeTrace.CreateDate = item.Workshop_CreatedTime;
objWorkShop.RecordChangeTrace.CreateUserCode = item.Workshop_CreatedPerson;
objWorkShop.RecordChangeTrace.ModifyDate = item.Workshop_LastChangedTime;
objWorkShop.RecordChangeTrace.ModifyUserCode = item.Workshop_LastChangedPerson;
lstWorkShop.Add(objWorkShop);
}
}
catch (Exception)
{
return null;
//throw;
}
return lstWorkShop;
}
/// <summary>
/// 在数据库中插入车间信息
/// </summary>
/// <param name="lstWorkShop"></param>
/// <returns></returns>
public bool Insert(List<WorkShop> lstWorkShop)
{
//在同一事务中,将lstWorkShop中的车间信息插入表tbl_WorkshopInfo中
//返回操作成功与否
using (TransactionScope scope = new TransactionScope())
{
try
{
foreach(WorkShop tem in lstWorkShop)
{
tbl_WorkshopInfo objTbl_WorkshopInfo = new tbl_WorkshopInfo()
{
Workshop_CreatedPerson = tem.RecordChangeTrace.CreateUserCode,
Workshop_CreatedTime = tem.RecordChangeTrace.CreateDate,
Workshop_LastChangedPerson = tem.RecordChangeTrace.ModifyUserCode,
Workshop_LastChangedTime = tem.RecordChangeTrace.ModifyDate,
Workshop_Werks = tem.Plant.Werks,
Workshop_WorkshopCode = tem.Code,
Workshop_WorkshopDes = tem.Description
};
//objTbl_WorkshopInfo.Workshop_Id = tem.Id;
//objTbl_WorkshopInfo.Workshop_Werks = tem.Plant.Werks;
//objTbl_WorkshopInfo.Workshop_WorkshopDes = tem.Plant.WerksDes;
//objTbl_WorkshopInfo.Workshop_CreatedTime = new HelperManager().GetDBCurrentDateTime2();
//objTbl_WorkshopInfo.Workshop_CreatedPerson = User.UserName;
//objTbl_WorkshopInfo.Workshop_LastChangedPerson = User.UserName;
//objTbl_WorkshopInfo.Workshop_LastChangedTime = new HelperManager().GetDBCurrentDateTime2();
objPlantDataClassesDataContext.tbl_WorkshopInfo.InsertOnSubmit(objTbl_WorkshopInfo);
}
objPlantDataClassesDataContext.SubmitChanges();
scope.Complete();
}
catch (Exception)
{
return false;
}
return true;
}
}
/// <summary>
/// 在数据库中删除车间信息
/// </summary>
/// <param name="lstWorkShop"></param>
/// <returns></returns>
public bool Delete(List<WorkShop> lstWorkShop)
{
//在同一事务中,在表tbl_WorkshopInfo中删除lstWorkShop中的车间信息
//返回操作成功与否
using (TransactionScope scope = new TransactionScope())
{
try
{
string sql = @"delete from tbl_WorkshopInfo where Workshop_Werks = {0} and Workshop_WorkshopCode = {1};
";
foreach (WorkShop tem in lstWorkShop)
{
objPlantDataClassesDataContext.ExecuteCommand(sql,tem.Plant.Werks,tem.Code);
}
scope.Complete();
}
catch (Exception)
{
return false;
}
return true;
}
}
/// <summary>
/// 在数据库中更新车间信息
/// </summary>
/// <param name="lstWorkShop"></param>
/// <returns></returns>
public bool Update(List<WorkShop> lstWorkShop)
{
//在同一事务中,在表tbl_WorkshopInfo中更新lstWorkShop中的车间信息
//返回操作成功与否
using (TransactionScope scope = new TransactionScope())
{
try
{
foreach (WorkShop tem in lstWorkShop)
{
tbl_WorkshopInfo objTbl_WorkshopInfo = objPlantDataClassesDataContext.tbl_WorkshopInfo.Where
(b => b.Workshop_Werks.Equals(tem.Plant.Werks) && b.Workshop_WorkshopCode.Equals(tem.Code)).First();
objTbl_WorkshopInfo.Workshop_WorkshopDes = tem.Description;
objTbl_WorkshopInfo.Workshop_CreatedTime = tem.RecordChangeTrace.CreateDate;
objTbl_WorkshopInfo.Workshop_CreatedPerson = tem.RecordChangeTrace.CreateUserCode;
objTbl_WorkshopInfo.Workshop_LastChangedPerson = tem.RecordChangeTrace.ModifyUserCode;
objTbl_WorkshopInfo.Workshop_LastChangedTime = tem.RecordChangeTrace.ModifyDate;
objPlantDataClassesDataContext.SubmitChanges();
}
scope.Complete();
}
catch (Exception)
{
return false;
//throw;
}
return true;
}
}