如何比较DB table中的时间和C#中的当前系统时间?
How to compare time in DB table and the current system time in C#?
我需要将 table 中的时间与当前系统时间进行比较,并需要弹出通知。但是我怎么能比较两者呢?
这是我的代码和数据库:
SqlCommand cmd = new SqlCommand("SELECT RestTime FROM FunTime WHERE UserID=1",conn);
String currenttime = DateTime.Now.ToString("HH:mm:ss tt");
if (cmd = currenttime)
{
}
您可以直接使用DateTime
而不是将其转换为String
并进行字符串比较:
SqlDataReader reader = cmd .ExecuteReader();
DateTime? dateFromDB = null;
while (reader.Read())
{
dateFromDB = Convert.ToDateTime(reader["RestTime"]);
}
if(dateFromDB.HasValue)
{
if(DateTime.Now.TimeOfDay == dateFromDB.TimeOfDay
{
// time equal
}
}
我需要将 table 中的时间与当前系统时间进行比较,并需要弹出通知。但是我怎么能比较两者呢? 这是我的代码和数据库:
SqlCommand cmd = new SqlCommand("SELECT RestTime FROM FunTime WHERE UserID=1",conn);
String currenttime = DateTime.Now.ToString("HH:mm:ss tt");
if (cmd = currenttime)
{
}
您可以直接使用DateTime
而不是将其转换为String
并进行字符串比较:
SqlDataReader reader = cmd .ExecuteReader();
DateTime? dateFromDB = null;
while (reader.Read())
{
dateFromDB = Convert.ToDateTime(reader["RestTime"]);
}
if(dateFromDB.HasValue)
{
if(DateTime.Now.TimeOfDay == dateFromDB.TimeOfDay
{
// time equal
}
}