简体   繁体   English

将 DateTime.Now 与存储在 SQL 中的值进行比较

[英]Comparing DateTime.Now to value stored in SQL

I am converting ADODB namespace code to SqlClient .我正在将ADODB命名空间代码转换为SqlClient I am trying to replace code that utilized recordset.Value .我正在尝试替换使用recordset.Value代码。 I am having problems converting this line of code.我在转换这行代码时遇到问题。 How can I subtract DateTime.Now from the value in the time_of_lock (Datetime data type) column in SQL ?如何从SQLtime_of_lock (日期时间数据类型)列中的值中减去DateTime.Now

else if (DateTime.Now - rs.Fields["time_of_lock"].Value < TimeSpan.FromMinutes(15))

I am assuming you can get two DateTime values from your code.我假设您可以从代码中获得两个DateTime值。 If so, try pop both into the following function:如果是这样,请尝试将两者都弹出到以下函数中:

private bool TimesApartNoMoreThan(DateTime first, DateTime second, int threshold) 
{
    return (second - first).TotalMinutes > threshold;
}

when you subtract two DateTimes, you get a TimeSpan .当您减去两个 DateTime 时, 您会得到一个 TimeSpan Having obtained that, you can represent it as whatever unit of duration you need ( in this case, minutes ):获得它后,您可以将其表示为您需要的任何持续时间单位( 在本例中为分钟):

From your statement, I am assuming rs.Fields["time_of_lock"] is of DateTime datatype.根据您的陈述,我假设 rs.Fields["time_of_lock"] 是 DateTime 数据类型。 If that is the case, please try this:如果是这种情况,请尝试以下操作:

DateTime.Now .Subtract(rs.Fields["time_of_lock"].Value).TotalMinutes < 15

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

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