我使用此代码按日期字段过滤(在表格中) 这是我的代码:
public double Mounthcost()
{
DateTime today = DateTime.Now;
PersianCalendar persianCalendar = new PersianCalendar();
int yearPersian = persianCalendar.GetYear(today);
int monthPersian = persianCalendar.GetMonth(today);
DateTime firstDayOfMonth = persianCalendar.ToDateTime(yearPersian, monthPersian, 1, 0, 0, 0, 0);
int daysInMonth = persianCalendar.GetDaysInMonth(yearPersian, monthPersian);
DateTime lastDayOfMonth = firstDayOfMonth.AddDays(daysInMonth - 1);
//try
//{
SqlDataReader DR = ExecuteReader(System.Data.CommandType.Text, "Select Sum(Price) From Tbl_Cost Where Dat Between" + firstDayOfMonth + "AND" + lastDayOfMonth);
if (DR.Read())
{
Mouth_Cost = Double.Parse(DR[0].ToString());
}
return Mouth_Cost;
//}
//catch
//{
// return 0;
//}
}
并出现此错误:
在预期条件的上下文中指定的非布尔类型的表达式,接近'介于10和#39之间。
答案 0 :(得分:2)
您缺少SQL查询中的空格
SqlDataReader DR = ExecuteReader(System.Data.CommandType.Text, "Select Sum(Price) From Tbl_Cost Where Dat Between " + firstDayOfMonth + " AND " + lastDayOfMonth);
注意BETWEEN和AND之后的空格。
答案 1 :(得分:1)
谢谢HABO :) 用这个:
SqlDataReader DR = ExecuteReader(System.Data.CommandType.Text, "Select Sum(Price) From Tbl_Cost Where Dat Between @F AND @L", new SqlParameter[]
{
new SqlParameter("@F", firstDayOfMonth),
new SqlParameter("@L", lastDayOfMonth),
}
是对的......