[英]Check if 1 hour has passed from SQL Server Datetime column string C#
我正在嘗試檢查是否從SQL Server數據庫中的字符串Datetime
過去了1個小時。 我嘗試了這段代碼,但是沒有用,或者我聽不清楚。
我想做的是獲取列Datetime值,並檢查是否從TimeDate.Now
傳遞了1小時。
這是我的代碼:
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();
}
}
}
}
一個簡單的方法就是
public bool HasAnHourPassed(DateTime compareTime)
{
var timePassed = DateTime.Now - compareTime;
return timePassed.TotalHours > 1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.