格式异常:字符串未被识别为有效的DateTime

时间:2011-09-09 12:14:09

标签: c# .net mysql winforms datetime

嗨我有从数据库中获取值并在图表控件中表示这些值的方法..

这是方法......

      public static void Hourlyattendence(System.DateTime startdate, System.DateTime enddate, string StartHour, string EndHour,
                                           out string[] Hours, out int[] Accepted, out int[] Refused)
     {
         int hours = 1 + int.Parse(EndHour) - int.Parse(StartHour);

         Hours = new string[hours];
         Accepted = new int[hours];
         Refused = new int[hours];

         int result = 0;

         for (int i = 0; i < hours; i++)
         {
             Accepted[i] = 0;
             Refused[i] = 0;
             Hours[i] = string.Format("{0:00}", int.Parse(StartHour) + i);
         }

         const string sql = @"SELECT COUNT('x') AS numVisits, visit_Status as Status, SUBSTRING(visit_Time,1,2) as visitHour
                              FROM visits
                              WHERE visit_Date BETWEEN @startdate AND @enddate
                              AND SUBSTRING(visit_Time,1,2) between @StartHour and @EndHour
                              GROUP BY SUBSTRING(visit_Time,1,2), visit_Status";

         var hourstable = new DataTable();

         using (var conn = new MySql.Data.MySqlClient.MySqlConnection(connectionstring))
         {
             conn.Open();

             var cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);

             var ds = new DataSet();
             var parameter = new MySql.Data.MySqlClient.MySqlParameter("@startdate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter.Direction = ParameterDirection.Input;
             parameter.Value = startdate;
             cmd.Parameters.Add(parameter);

             var parameter2 = new MySql.Data.MySqlClient.MySqlParameter("@enddate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter2.Direction = ParameterDirection.Input;
             parameter2.Value = enddate;
             cmd.Parameters.Add(parameter2);

             var parameter3 = new MySql.Data.MySqlClient.MySqlParameter("@StartHour", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter3.Direction = ParameterDirection.Input;
             parameter3.Value = StartHour;
             cmd.Parameters.Add(parameter3);

             var parameter4 = new MySql.Data.MySqlClient.MySqlParameter("@EndHour", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter4.Direction = ParameterDirection.Input;
             parameter4.Value = EndHour;
             cmd.Parameters.Add(parameter4);

             var da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);

             da.Fill(ds);
             try
             {
                 hourstable = ds.Tables[0];

             }
             catch
             {
                 hourstable = null;
             }
         }
         if (hourstable != null)
         {
             for (int i = 0; i < hourstable.Rows.Count; i++)
             {
                 if (int.TryParse(hourstable.Rows[i]["numVisits"].ToString(), out result) && int.TryParse(hourstable.Rows[i]["visitHour"].ToString(), out hours))
                 {
                     hours -= int.Parse(StartHour);
                     if (hourstable.Rows[i]["Status"].ToString().ToUpper() == "ACCEPTED")
                     {
                         Accepted[hours] = result;
                     }
                     else
                     {
                         Refused[hours] = result;
                     }
                 }

             }

         }

     }

我在下面的代码中调用了上面的方法......这就是我在图表控件中表示数据的地方....

    private void KpiHourlAttendenceForm_Load(object sender, EventArgs e)
    {
        Drawkpihourlyattendence(dtpStartDate.Value, dtpEnddate.Value);
    }

    public void Drawkpihourlyattendence(DateTime startdate, DateTime enddate)
    {
        try
        {
            KpiHourlyattaendencechart.Series.Clear();

            Series acceptedSeries = KpiHourlyattaendencechart.Series.Add("Accepted");
            Series rejectedSeries = KpiHourlyattaendencechart.Series.Add("Refused");

            string[] xValues;
            int[] yValues;
            int[] yValues2;

            KpiData.Hourlyattendence(startdate, enddate, "06", "22", out xValues, out yValues, out yValues2);

            //blah
           //blah
           ///blah
       }
       catch(FormatException e)
      {
            Console.WriteLine(e.Message);

       }    

得到异常:格式异常字符串未被识别为有效的日期时间。

任何人都可以帮忙......

非常感谢提前......

3 个答案:

答案 0 :(得分:1)

将开始/结束时间更改为整数SQL参数类型,并将SUBSTRING(visit_Time,1,2)转换为您在BETWEEN @StartHour和@EndHour

中执行的行中的整数

答案 1 :(得分:1)

StartHourEndHour不是DateTime

此外,正如@Ryan Wright指出的那样,您的between过滤器无法按预期使用字符串。

答案 2 :(得分:0)

我认为您必须更改@StartHour@EndHour的参数类型,因为它们是字符串。