后台模块--车次增删改查
1、今日完成任务:
(1)车次管理界面
(2)车次增删改查
2、核心源码:
public class TicketsDAL
{
carDBDataContext db = new carDBDataContext();
/// <summary>
/// 查询车次信息
/// </summary>
/// <param name="filed1">起始站</param>
/// <param name="filed2">到达站</param>
/// <returns>车次信息</returns>
public object selectTickets(string filed1=null,string filed2=null)
{
object ticket = null;
if (filed1 != null && filed2 != null)
{
ticket = db.Tickets.Where(m => m.filed1 == filed1 && m.filed2 == filed2 && m.delstatus == false).ToList();
}
else
{
ticket = db.Tickets.ToList();
}
return ticket;
}
/// <summary>
/// 根据ID查询车次信息
/// </summary>
/// <param name="id">车次ID</param>
/// <returns>车次实体</returns>
public Tickets selectTicketByID(int id)
{
return db.Tickets.Where(m => m.ID == id).FirstOrDefault();
}
/// <summary>
/// 新增车次信息
/// </summary>
/// <param name="u">车次实体</param>
/// <returns>是否新增成功true/false</returns>
public bool AddTickets(Tickets t)
{
int count = db.Tickets.Count(); //初始车次数量
db.Tickets.InsertOnSubmit(t); //新增
db.SubmitChanges(); //提交并更改数据库
if (count < db.Tickets.Count()) //如果初始车次数量 < 新增后数量
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 删除车次信息
/// </summary>
/// <param name="id">车次ID</param>
/// <returns>是否删除成功true/false</returns>
public bool DelTickets(int id)
{
int count = db.Tickets.Count(); //初始车次数量
//根据id获取要删除的车次实体
Tickets u = db.Tickets.Where(m => m.ID == id).FirstOrDefault();
if (u != null)
{
db.Tickets.DeleteOnSubmit(u); //删除
db.SubmitChanges();//提交并更改数据库
}
if (count > db.Tickets.Count()) //如果初始车次数量 > 删除后数量
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 修改车次信息
/// </summary>
/// <param name="u">车次实体</param>
/// <returns>是否修改成功true</returns>
public bool EditTickets(Tickets t)
{
Tickets t1 = db.Tickets.Single(m => m.ID == t.ID); //找到数据库中要修改的实体对象
t1.ID = t.ID;
t1.carNo = t.carNo;
t1.filed1 = t.filed1;
t1.filed2 = t.filed2;
t1.beginDate = t.beginDate;
t1.beginTime = t.beginTime;
t1.mileage = t.mileage;
t1.carType = t.carType;
t1.price = t.price;
t1.amount = t.amount;
t1.remark = t.remark;
t1.delstatus = t.delstatus;
db.SubmitChanges(); //提交并更改数据库
return true;
}