【问题标题】:C# Get data between date and hour from MySQL tableC# 从 MySQL 表中获取日期和小时之间的数据
【发布时间】:2016-07-20 08:55:21
【问题描述】:

我正在尝试从我的数据库中打印一个表,但我想使用此块代码对其进行过滤,我想做的是在两个小时之间打印数据,但我不知道是小时的输入格式是正确的,所以这是代码:

    string horaI=null;
    string horaF=null;
    string[] hr1 = null;
    string[] hr2 = null;

    on load....
    dateTimePicker1.CustomFormat = "HH:mm tt"; // Only use hours and minutes
    horaI = dateTimePicker1.Value.ToString("HH:mm tt");
    hr1 = horaI.Split();

    string connectionstring = null;
    string sql = null;
    string data = null;

    connectionstring = "server=127.0.0.1; database=gimnasio5; uid=root; pwd=0000000000;";

        sql = "SELECT IdMembresia, Nombre, Apellido, Tipo, Fecha_Inicio,
   Fecha_Vencimiento, Inscripcion, Total,Impreso_Corte FROM membresia where
   Impreso_Corte='No impreso' or (Fecha_Membresia between @d1 and @d2 and
   Hora_Membresia between @d3 and @d4) order by gimnasio5.membresia.IdMembresia;";

        var dtable = new DataTable("membresia");
        var conn = new MySql.Data.MySqlClient.MySqlConnection(connectionstring);
        var cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);
        var dscmd = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);

        using (conn)
        {

            var param = new MySql.Data.MySqlClient.MySqlParameter("@d1", MySql.Data.MySqlClient.MySqlDbType.Date);
            param.Direction = ParameterDirection.Input;
            param.Value = DateTime.Today;
            cmd.Parameters.Add(param);
            param = new MySql.Data.MySqlClient.MySqlParameter("@d2", MySql.Data.MySqlClient.MySqlDbType.Date);
            param.Direction = ParameterDirection.Input;
            param.Value = DateTime.Today;
            cmd.Parameters.Add(param);

 //The error can be here because when I use it with dates only it works fine
 //but when I add this part of code, fails. 

            param = new MySql.Data.MySqlClient.MySqlParameter("@d3", MySql.Data.MySqlClient.MySqlDbType.Time);
            param.Direction = ParameterDirection.Input;
            param.Value = hr1[0];  //Convert.ToDateTime(hr1[0]).ToString("HH:mm");
            cmd.Parameters.Add(param);
            param = new MySql.Data.MySqlClient.MySqlParameter("@d4", MySql.Data.MySqlClient.MySqlDbType.Time);
            param.Direction = ParameterDirection.Input;
            param.Value = hr2[0];  //Convert.ToDateTime(hr2[0]).ToString("HH:mm");
            cmd.Parameters.Add(param);

            conn.Open();
            dscmd.Fill(dtable);
        }

但我得到了错误:MySql.Data.dll 中发生了“MySql.Data.MySqlClient.MySqlException”类型的异常,但未在用户代码中处理

附加信息:命令执行过程中遇到致命错误。

当我尝试填充 MySqlAdapter 对象时出现错误:

dscmd.Fill(dtable);

我认为这是我输入时间的格式,但正如您在我使用它的格式的代码中看到的那样,但两者都不起作用,并返回相同的错误代码。 我在MySQL数据库中的列设置为保存时间类型,所以问题不在表中。

数据库中的小时是这样保存的,列是时间类型:

12:03:00

21:34:00

提前致谢。

表结构

   CREATE TABLE `membresia` (
   `IdMembresia` int(11) NOT NULL AUTO_INCREMENT,
   `Nombre` varchar(100) NOT NULL,
   `Apellido` varchar(100) NOT NULL,
   `Tipo` varchar(100) NOT NULL,
   `Fecha_Inicio` date NOT NULL,
   `Fecha_Vencimiento` date NOT NULL,
   `Inscripcion` varchar(20) DEFAULT NULL,
   `Estado_membresia` varchar(15) NOT NULL,
   `Fecha_modificacion` date NOT NULL,
   `Total` decimal(10,2) NOT NULL,
   `Nota` varchar(200) DEFAULT NULL,
   `Fecha_Membresia` date NOT NULL,
   `Impreso_Corte` varchar(20) NOT NULL,
   `IdSocio` int(11) DEFAULT NULL,
   `Hora_Membresia` time NOT NULL,
   PRIMARY KEY (`IdMembresia`),
   KEY `L_Id2` (`IdSocio`),
   KEY `F_Nombre` (`Nombre`),
   KEY `F_Apelli` (`Apellido`),
   CONSTRAINT `F_Apelli` FOREIGN KEY (`Apellido`) REFERENCES `socios` (`Apellido`) ON DELETE CASCADE ON UPDATE CASCADE,
   CONSTRAINT `F_Nombre` FOREIGN KEY (`Nombre`) REFERENCES `socios` (`Nombre`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `L_Id2` FOREIGN KEY (`IdSocio`) REFERENCES `socios` (`IdSocio`) ON DELETE CASCADE ON UPDATE CASCADE)
   ENGINE=InnoDB DEFAULT CHARSET=utf8;

【问题讨论】:

  • 你能和我们分享一下你的表结构吗?
  • 是的,我写的结构现在你可以看到了
  • 你不需要 Using(conn) 这个方法...只需根据你的应用编辑这个查询。你需要从 datetimepicker 中消磨时间......告诉我你是否有问题。
  • 是的,我正在擦除代码
  • 您需要 4 个日期时间选择器,因为您想检查日期范围的第一个和第二个时间范围

标签: c# mysql datetime-format time-format


【解决方案1】:

这样的代码:

        SqlConnection conn = new SqlConnection("server=127.0.0.1; database=gimnasio5; uid=root; pwd=0000000000;");

        conn.Open();

        string query = string.Format(@"SELECT IdMembresia, Nombre, Apellido, Tipo, Fecha_Inicio,
                       Fecha_Vencimiento, Inscripcion, Total, Impreso_Corte FROM membresia where
                       Impreso_Corte = 'No impreso' or(Fecha_Membresia between '{0}' and '{1}' and
                       Hora_Membresia between '{2}' and '{3}') order by gimnasio5.membresia.IdMembresia", dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString(), dateTimePicker3.Value.ToString("hh:mm:ss"), dateTimePicker4.Value.ToString("hh:mm:ss"));

        SqlCommand cmd = new SqlCommand(query, conn);

        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());
        conn.Close();
        return dt;

【讨论】:

  • 它现在没有给我错误但我没有得到数据
  • 我认为输入参数值和数据库值不匹配。所以检查输入参数和数据库值...
  • 那我去看看
  • 我发现错误是字符串日期格式 (hh:mm:ss) 我需要 24 小时格式 (HH:mm:ss)
  • 将你的 datetimepicker 设置为像图片一样(点击链接)。并得到时间写 like=>> string time = dateTimePicker1.Value.ToString("HH:mm:ss"); Click here to see DateTimePicker as TimePicker
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 2020-11-11
  • 1970-01-01
相关资源
最近更新 更多