如何将数据库中保存的时间与当前系统时间进行比较? [英] How can I compare a time saved in database with the current system time?
问题描述
我有一个名为Events的表,其中有很多列,其中一个是时间表示在24/04/2015 12:00:00 AM格式。
我只需要获取时间列,每当指定的时间到达系统时,都会显示一条弹出消息。
这是我的代码(以主表格加载的方法)
我试过了这不会弹出一条消息,PLZ帮助。
I have a table called Events where there are many columns and one of them is Time represented in 24/04/2015 12:00:00 AM format.
All I need is to fetch the time column and whenever the specified time arrives in system, a popup message should be displayed.
Here is My code (A method coded in the main form load)
I tried this but it doesnt popup a message, plz help.
public void Reminders()
{
SqlCeConnection conn = new SqlCeConnection("Data Source=E:\\Project Final_Year\\Project Final\\MyProject\\ERU Implementation\\VirtualPetedit\\VirtualPet\\MyDB.sdf");
string query = "SELECT time FROM Events";
SqlCeDataReader r;
DateTime thisDay = DateTime.Today;
try
{
conn.Open();
SqlCeCommand command = new SqlCeCommand(query, conn);
r = command.ExecuteReader();
//if (r.HasRows)
//{
while (r.Read())
{
if (r.GetDateTime(0) == thisDay )
{
VirtualPet.Popup popup = new VirtualPet.Popup();
popup.Reminder();
}
}
//}
/* else
{
MessageBox.Show("No Reminder times currently");
}*/
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
推荐答案
Hi
今日格式/数据类型的输出不等于您在数据库中拥有什么,这意味着在数据库中,如果您想将此格式/数据类型与当前日期和时间进行比较(在本例中为今天),您可以使用此格式的日期和时间4/04/2015 12:00:00 AM你需要这样做:
Hi The output of thisday format/datatype is not equle with what ever you have in your database, which means in database you have Date&Time with this format 4/04/2015 12:00:00 AM if you want to compare this format/datatype with current Date&Time (in this case thisday) you need to do like this:
DateTime thisDay = DateTime.Today;
DateTime tag = r.GetDateTime(0);
int result = DateTime.Compare(thisDay, tag);
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";
其他相关实用代码:
Other related useful codes:
DateTime myDateTime = DateTime.Now;
string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss");
相关链接:
http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/SqlDataReaderGetStringGetDateTime.htm [ ^ ]
https:// msdn .microsoft.com / zh-cn / library / system.datetime.compare(v = vs.110).aspx [ ^ ]
http://www.dotnetperls.com/datetime-format [<小时ef =http://www.dotnetperls.com/datetime-formattarget =_ blanktitle =New Window> ^ ]
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110) .aspx [ ^ ]
R,
Aydin
Related Links:
http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/SqlDataReaderGetStringGetDateTime.htm[^]
https://msdn.microsoft.com/en-us/library/system.datetime.compare(v=vs.110).aspx[^]
http://www.dotnetperls.com/datetime-format[^]
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^]
R,
Aydin
这篇关于如何将数据库中保存的时间与当前系统时间进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!