我有一个任务,我必须根据 DateFrom 和 DateTo 过滤记录。 在 mysql 中,日期 fromat 是这样的 '2014-07-18 20:03:08'。 在前端我使用 DatetimePicker 到文本框..datetimepicker 格式结果给出 '2014/07/09 14:00'。 因此无法比较数据库日期格式和日期时间格式。
我想要带秒的日期时间选择器..plz 帮助我
最佳答案
DateTimePicker
在 Value
属性中存储一个 DateTime
。您可以向其中添加所需的秒数:
DateFrom.Value = DateFrom.Value.AddSeconds(seconds);
datetimepicker format results gives '2014/07/09 14:00'.
不要使用字符串作为 MySql 中日期时间列的参数。而是使用 sql 参数并直接传递 DateTime
:
// add seconds if you want, but i don't see any reason for it
// maybe you want to include the To-date, then you can add a day - one second
DateTo.Value = DateTo.Value.AddDays(1).AddSeconds(-1);
using (MySqlConnection conn = new MySqlConnection("connStr"))
{
try
{
conn.Open();
string sql = @"SELECT t.From, t.To, OtherColumns...
FROM TableName t
WHERE FROM >= @From AND To <= @To";
using(MySqlCommand cmd = new MySqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@From", DateFrom.Value);
cmd.Parameters.AddWithValue("@To", DateTo.Value);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// ...
}
}
}
} catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
关于c# - 如何在 asp.net 中获取带秒数的 Datetimepicker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24950060/