數據訪問層----數據持久層
using System;
using System.Data;
namespace train
...{
/**//// <summary>
/// Schedule:負責與數據層的交互
/// </summary>
public class Schedule
...{
private DataBaSEOperate db;
private DataTable table;
private DataTable tableresult;
private DataTable tabledetails;
public Schedule()
...{
db=new DataBaSEOperate();
table=new DataTable();
}
/**//// <summary>
/// 根據起點站、終點站搜索列車信息
/// </summary>
/// <param name="fromcity">起點站</param>
/// <param name="tocity">終點站</param>
/// <returns>返回列車信息的表DataTable</returns>
public DataTable SearchScheduleList(string fromcity,string tocity)
...{
string sql="select 車次=code,起點站=fromcity,終點站=tocity,發車時間=LeaveTime,"+
"到達時間=ArriveTime,列車信息=trainType,時速=speed,距離=distance "+
"from T_train where fromcity=''"+fromcity+"'' and toCity=''"+ tocity+"''";
table=db.search(sql);
tableresult=CalculatePrice(table);
return tableresult;
}
/**//// <summary>
/// 計算票價
/// </summary>
&n
bsp; /// <param name="table">需要計算票價的DataTable</param>
/// <returns>返回已經計算票價的DataTable</returns>
public DataTable CalculatePrice(DataTable tables)
...{
double price;
tables.Columns.Add("價格",typeof(int));
for(int i=0;i<table.Rows.Count;i++)
...{
price=(double.Parse(tables.Rows[i]["距離"].ToString()))/100*10;
if((double.Parse(tables.Rows[i]["距離"].ToString()))%100!=0)
...{
price=price+10;
}
switch(tables.Rows[i]["列車信息"].ToString())
...{
case "特快":
price=price*2;
break;
case "空調":
price=price*2;
break;
case "快車":
price=price*1.2;
break;
default:
break;
}
tables.Rows[i]["價格"]=price;
}
return tables;
}
/**//// <summary>
/// 顯示用用戶選擇的列車的詳細信息
/// </summary>
/// <param name="code">列車的車次</param>
/// <returns>返回列車信息的表DataTable</returns>
public DataTable SearchMidScheduleList(string code)
...{
string sql="select 車次=code,經停站=cityname,發車時間=leavetime,裡程=distance,天數=days from T_traindetails where code=''"+code+"''";
tabledetails=db.search(sql);
return tabledetails;
}
/**//// <summary>
/// 根據終點站得到中間站的信息
/// </summary>
/// <param name="tocity">終點站的名字</param>
/// <returns>返回包含中間站信息的DataTable</returns>
public DataTable SearchMidList(string tocity)
...{
string sql="select distinct cityname from T_traindetails where cityname not like ''%"+tocity+"%'' and code in"+
"(select code from T_traindetails where cityname like ''%"+tocity+"%'')";
return db.search(sql);
}
/**//// <summary>
/// 根據選擇的中間站返回起點站、中間站、終點站的詳細信息
/// </summary>
/// <param name="fromcity">起點站</param>
/// <param name="midcity">中間站</param>
/// <param name="finalcity">終點站</param>
/// <returns>返回包含起點站、中間站、終點站的詳細信息DataTable</returns>
public DataTable SearchMidStation(string fromcity,string midcity,string finalcity)
...{
string sql="select 車次=code,起點站=fromcity,終點站=tocity,發車時間=LeaveTime,"+
"到達時間=ArriveTime,列車信息=trainType,時速=speed,距離=distance "+
"from T_train where fromcity=''"+fromcity+"'' and toCity=''"+ midcity+"''"+
" union "+
"select 車次=code,起點站=fromcity,終點站=tocity,發車時間=LeaveTime,"+
"到達時間=ArriveTime,列車信息=trainType,時速=speed,距離=distance "+
"from T_train where fromcity=''"+midcity+"'' and toCity=''"+ finalcity+"''";
table=db.search(sql);
tableresult=CalculatePrice(table);
return tableresult;
}
/**//// <summary>
/// 根據列車的車次獲得列車的詳細信息
/// </summary>
/// <param name="code">列車的車次</param>
/// <returns>返回列車信息的表DataTable</returns>
public DataTable SearchScheduleByCode(string code)
...{
string sql="select 車次=code,起點站=fromcity,終點站=tocity,發車時間=LeaveTime,"+
"到達時間=ArriveTime,列車信息=trainType,時速=speed,距離=distance "+
"from T_train where code=''"+code+"''";
table=db.search(sql);
tableresult=CalculatePrice(table);
return tableresult;
}
public DataTable GetTableResult()
...{
return this.tableresult;
}
public DataTable GetTableDetails()
...{
return this.tabledetails;
}
}
}