简体   繁体   中英

Syntax error (missing operator) in query expression in C#

string sqlStatement = "select ID, Checkintime, RoomPrice, OrderNo from Orders where RoomType='" + selectedRoom.Key + "' and RoomNumber=" + selectedRoom.Value + " and Checkintime>="+dateOnly+" and CheckinTime<'"+endingTime+"'"; 

myAccessCommand = new OleDbCommand(sqlStatement, myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myDataAdapter.Fill(myDataSet, "Orders");

I have syntax error in the SQL statement line but I am not sure what went wrong that caused an syntax exception.

Short fix: change

Checkintime>="+dateOnly+" 

to

Checkintime>='"+dateOnly+"'

(you forgot the tick marks in the first reference to Checkintime )


Much better: Use a parameterized query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM