[英]Check if 1 hour has passed from SQL Server Datetime column string C#
I'm trying to check if 1 hour ha passed from string Datetime
that I have in SQL Server database. 我正在尝试检查是否从SQL Server数据库中的字符串Datetime
过去了1个小时。 I tried this code but it didn't work or I didn't understand it right. 我尝试了这段代码,但是没有用,或者我听不清楚。
What I'm trying to do is to take the column Datetime value and check if passed 1 hour from TimeDate.Now
. 我想做的是获取列Datetime值,并检查是否从TimeDate.Now
传递了1小时。
Here is my code : 这是我的代码:
using (var GetLastPost = new SqlCommand("SELECT PostDate FROM Accs2 WHERE Username LIKE @user1", con))
{
GetLastPost.Parameters.AddWithValue("@user1", ecid);
using (var LastReader = await GetLastPost.ExecuteReaderAsync())
{
if (!LastReader.HasRows || !LastReader.Read())
{
}
else
{
DateTime k = Convert.ToDateTime(LastReader["PostDate"].ToString());
if (k > DateTime.Now)
{
MessageBox.Show("Your Money is in the Box");
}
else
{
MessageBox.Show("1 Hour Not Passed yet");
con.Close();
}
}
}
}
A simple method would be something like 一个简单的方法就是
public bool HasAnHourPassed(DateTime compareTime)
{
var timePassed = DateTime.Now - compareTime;
return timePassed.TotalHours > 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.