2011-09-09 104 views
0

您好我有方法得到的值形成數據庫,並代表圖表控件的值..格式異常:字符串未被識別爲有效的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); 

     }  

得到了例外:F ormat異常字符串未被識別爲有效的DateTime。

將在此任何一個請幫助.....

許多在此先感謝...

回答

0

我認爲你必須更改參數類型@StartHour@EndHour因爲它們是字符串。

1

變化的開始/結束時間爲整數的SQL參數類型和鑄SUBSTRING(visit_Time,1,2)的一個整數,你做你的線之間@StartHour和@EndHour

1

StartHourEndHourDateTime

此外,正如@Ryan Wright指出的,您的between過濾器無法按預期的方式使用字符串。

相關問題