我如何得到一个唯一的超时从访问数据库在c#

本文关键字:超时 访问 数据库 唯一 一个 何得 | 更新日期: 2023-09-27 18:06:02

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+"");
            }

我尝试做这个方法,但它显示了一个异常:{"数据类型不匹配的标准表达式。"},这样它就不会在不同的日子里选出两个相同的时间。

我如何得到一个唯一的超时从访问数据库在c#

无论是从问题还是从代码中都不清楚您想要做什么。然而,代码有潜在的错误,你不是使用参数,而是制作一个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");