简体   繁体   中英

OleDbDataAdapter UPDATE query comparing date type with string

I have an OleDbDataAdapte r that is doing an UPDATE on a database. On the table that I am updating I have a column named "Temp_date" that holds dates in the mm/dd/yyyy format. Is there any way I can compare the string from the table with an actual DateTime (current date)? My purpose is that if the date stored in the table is lower than the current date then to have the old value deleted.I would really appreciate your help or suggestions!

Here is what my code looks like:

OleDbDataAdapter adapter3 = new OleDbDataAdapter();
adapter3.UpdateCommand = conexiuneBD.CreateCommand();
adapter3.UpdateCommand.CommandText = "UPDATE table SET Occupied=No, Temp_date=? WHERE Temp_date<?";
adapter3.UpdateCommand.Parameters.AddWithValue("p1", DBNull.Value);
adapter3.UpdateCommand.Parameters.AddWithValue("p2", current_date);

What about converting the date first and just passing the constant value to compare?

OleDbDataAdapter adapter3 = new OleDbDataAdapter();
adapter3.UpdateCommand = conexiuneBD.CreateCommand();
string convertedDate = ConvertDateToAccessFormat(current_date);
adapter3.UpdateCommand.CommandText = "UPDATE table SET Occupied=No, Temp_date=? WHERE Temp_date<" + convertedDate;
adapter3.UpdateCommand.Parameters.AddWithValue("p1", DBNull.Value);
adapter3.UpdateCommand.Parameters.AddWithValue("p2", current_date);

Or you could make it a two part process. First, get the current value of Temp_date from the database, and only if it is less than your prospective new date would you call the update code.

Or you could use the access string-to-date conversion:

UPDATE table SET Occupied=No, Temp_date=? WHERE cDate(Format(Temp_date,"yyyy-MM-dd hh:mm:ss")) < ?

And BTW, if Temp_date is stored as a string, aren't you going to have to send it in as a string? In that case you'll have to convert both values in order to compare them as dates. Something like this?

UPDATE table SET Occupied=No, Temp_date=? WHERE cDate(Format(Temp_date,"yyyy-MM-dd hh:mm:ss")) < cDate(Format(?,"yyyy-MM-dd hh:mm:ss"))

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