简体   繁体   English

OleDbDataAdapter UPDATE查询将日期类型与字符串进行比较

[英]OleDbDataAdapter UPDATE query comparing date type with string

I have an OleDbDataAdapte r that is doing an UPDATE on a database. 我有一个OleDbDataAdapte r,它正在对数据库执行UPDATE On the table that I am updating I have a column named "Temp_date" that holds dates in the mm/dd/yyyy format. 在我要更新的表上,有一个名为"Temp_date"的列,该列以mm/dd/yyyy格式保存日期。 Is there any way I can compare the string from the table with an actual DateTime (current date)? 有什么方法可以将表中的字符串与实际的DateTime(当前日期)进行比较? 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. 首先,从数据库中获取Temp_date的当前值,只有当它小于预期的新日期时,才调用更新代码。

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? 顺便说一句,如果将Temp_date存储为字符串,您是否不必将其作为字符串发送? 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=? UPDATE表SET Occupied =否,Temp_date =? WHERE cDate(Format(Temp_date,"yyyy-MM-dd hh:mm:ss")) < cDate(Format(?,"yyyy-MM-dd hh:mm:ss")) 在哪里cDate(Format(Temp_date,“ yyyy-MM-dd hh:mm:ss”))<cDate(Format(?,“ yyyy-MM-dd hh:mm:ss”))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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