【发布时间】:2012-02-27 09:05:50
【问题描述】:
我有一个表价格周期 tblPricePeriod,其中我有 Amount 在特定公寓的两个日期之间收取费用。
我还有StartDate 和EndDate 列的价格时段,ApartmentId。
下面是我的表结构:
现在我要计算收取预订天数的总金额。
例如,如果某人从 2 月 4 日到 2 月 7 日逗留,那么他必须按照以下方案付款:
4- 10
5- 10
6- 10
7 - nothing as this is checkout date
-----------------------------------------------------
totamt--- 30$
我有startdate 和enddate 和apartmentid 作为参数,我使用的是ms access db。
现在我正在使用 c# 代码通过后续代码遍历它,但没有成功。
我一一获取所有日期的金额并将它们添加到一个全局变量中。
代码如下:
decimal amount = 0;
// loop to traverse all days between start date and end date
for (DateTime d = Convert.ToDateTime(StartDate); d < Convert.ToDateTime(EndDate); d = d.AddDays(1))
{
string priceCalc = "select Amount from tblPricePeriod where ApartmentId=" + ApartmenId + " and cdate(StartDate) <='" + d.ToShortDateString() + "' and cdate(EndDate) >= '" + d.ToShortDateString() + "'";
DataSet dsPriceCalc = SqlHelper.ExecuteDataset(Connection.ConnectionString, CommandType.Text, priceCalc);
if (dsPriceCalc.Tables[0].Rows.Count > 0)
{
amount = amount + Convert.ToDecimal(dsPriceCalc.Tables[0].Rows[0]["Amount"]);
}
}
【问题讨论】:
-
我不明白问题是什么?你得到一个错误或错误的总和?此外,如果您不希望客户在结帐日期付款,那么您应该在结帐日期使用
>而不是>=。 -
错误的模拟没有来自查询的数据并且数量保持为零
-
那些 'StartDate' 和 'EndDate' 列,它们有 'date' 类型吗? Access 不需要“#”作为日期分隔符吗?
-
能否尝试定位问题?
priceCalc、dsPriceCalc或amount是否包含任何数据? -
是的,他们有数据,因为我尝试使用调试器进行快速查看