c# - 如何在 asp.net 中获取带秒数的 Datetimepicker?

标签 c# mysql asp.net

我有一个任务,我必须根据 DateFrom 和 DateTo 过滤记录。 在 mysql 中,日期 fromat 是这样的 '2014-07-18 20:03:08'。 在前端我使用 DatetimePicker 到文本框..datetimepicker 格式结果给出 '2014/07/09 14:00'。 因此无法比较数据库日期格式和日期时间格式。

我想要带秒的日期时间选择器..plz 帮助我

最佳答案

DateTimePickerValue 属性中存储一个 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/

相关文章:

c# - 寻找一种存储父子孙结构的方法

MySQL查询: GROUP BY - get specific row (not always the first one)

javascript - jQuery $(document).ready 和 UpdatePanels?

jquery - 平衡 asp.net 服务器端验证与客户端 jQuery 验证

c# - 引用 Unity3D 中的众多协程之一?

c# - 如何添加应用栏分隔符

c# - 正在卸载在单独的 DLL 中具有逻辑的 .NET Web 项目

mysql - 网页不显示图表或任何错误

mysql - 在 MySQL 中删除表是否也会删除索引?

c# - 指定的字符串不是电子邮件地址所需的格式。发送电子邮件时