OleDbConnection myAccessConn = myAccessConnection();
OleDbCommand myAccessCommand = new OleDbCommand();
DataSet myDataSet = new DataSet();
string orderId = null, Checkintime=null, RoomPrice=null, ReceiptNo=null;
decimal totalfoodamount = 0;
try
{
myAccessConn.Open();
DateTime startTime = DateTime.Now.ToLocalTime();
DateTime dateOnly = startTime.Date;
DateTime endDateOnly = DateTime.Now.AddDays(1).ToLocalTime().Date;
string sqlStatement = "select ID,Checkintime, RoomPrice, OrderNo from Orders where RoomType='" + selectedRoom.Key + "' and RoomNumber =" + selectedRoom.Value + " and Checkintime between '" + dateOnly +"' and '"+endDateOnly+"'";
myAccessCommand = new OleDbCommand(sqlStatement, myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myDataAdapter.Fill(myDataSet, "Orders");
DataTableCollection dta = myDataSet.Tables;
DataColumnCollection drc = myDataSet.Tables["Orders"].Columns;
DataRowCollection dra = myDataSet.Tables["Orders"].Rows;
foreach (DataRow dr in dra)
{
orderId= dr[0].ToString();
Checkintime= dr[1].ToString();
RoomPrice= dr[2].ToString();
ReceiptNo= dr[3].ToString();
Console.WriteLine("orderid: "+orderId+", Checkintime: "+Checkintime+", RoomPrice: "+RoomPrice+", ReceiptNo: "+ReceiptNo+"");
}
我尝试过这种方法,但它显示了一个例外:{"标准表达式中的数据类型不匹配。"}因此它不会在不同的日期挑出2个相同的时间。
答案 0 :(得分:0)
目前尚不清楚你在问题和代码中都没有尝试做什么。但是代码有潜在的错误,你没有使用参数,而是使用+ ... +制作一个SQL字符串,这是一个禁忌。我修改了部分代码但由于无法理解你在做什么而无法进一步:
DateTime startTime = DateTime.Now.ToLocalTime().Date;
DateTime endTime = DateTime.Now.AddDays(1).ToLocalTime().Date;
string sqlStatement = @"select ID,Checkintime, RoomPrice, OrderNo
from Orders
where RoomType=@roomType and
RoomNumber = @roomNumber and
Checkintime >= @start and CheckinTime < @end";
myAccessCommand = new OleDbCommand(sqlStatement, myAccessConn);
myAccessCommand.Parameters.AddWithValue("@roomType", selectedRoom.Key);
myAccessCommand.Parameters.AddWithValue("@roomNumber", selectedRoom.Value);
myAccessCommand.Parameters.AddWithValue("@start", startTime);
myAccessCommand.Parameters.AddWithValue("@end", endTime);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand,myAccessConn);
myDataAdapter.Fill(myDataSet, "Orders");