2016-04-22 115 views
0

Hai我必須從一個表添加細節到另一個應該在日期之內的細節。這些日期是從文本框中讀取的。MySql.Data.MySqlClient.MySqlException:不正確的日期時間值

但我發現了錯誤:

"An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll but was not handled in user code 
Additional information: Incorrect datetime value: '11/25/2015 12:00:00 AM' for column 'debissuedate' at row 1" 

第一個表是t_bondridapp與字段:ID,CANCODE,canname,debissuedate ...等 我必須從這個表複製到新的命名作爲bondlocal與字段: bondid,cancode,canname,bonddate。 我使用的代碼

public class DBConnection 
     { 
      private DBConnection() 
      { 

      } 
      private string dbname = string.Empty; 
      public string DBName 
      { 
       get { return dbname;} 
       set { dbname = value;} 

      } 
      public string Password { get; set; } 
      private MySqlConnection mycon = null; 
      public MySqlConnection Connection 
      { 
       get { return mycon; } 
      } 
      private static DBConnection _instance = null; 
      public static DBConnection Instance() 

      { 
       if(_instance==null) 
        _instance=new DBConnection(); 
       return _instance; 
      } 
      public bool IsConnect() 
      { 
       bool result = true; 
       if(mycon==null) 
       { 
        if (String.IsNullOrEmpty(dbname)) 
         result = false; 
        string constr = string.Format("server=localhost;user id=root;password=mysql;database=pnys;",dbname); 
        mycon = new MySqlConnection(constr); 
        mycon.Open(); 
        result = true; 
       } 
       return result; 
      } 
      public void Close() 
      { 
       mycon.Close(); 
      } 
     } 




     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 



     protected void Button1_Click1(object sender, EventArgs e) 
     { 
      MySqlDateTime fdate =new MySqlDateTime(DateTime.Parse(TextBox3.Text)); 
      MySqlDateTime sdate = new MySqlDateTime(DateTime.Parse(TextBox4.Text)); 
      var dbCon = DBConnection.Instance(); 
      dbCon.DBName = "pnys"; 
      if (dbCon.IsConnect()) 
      { 
       string query = "INSERT INTO bondlocal (cancode,canname,bonddate) SELECT t_bondridapp.cancode,t_bondridapp.canname,t_bondridapp.debissuedate FROM t_bondridapp WHERE debissuedate>='" + fdate + "'AND debissuedate<='" + sdate + "'"; 
       MySqlCommand cmd = new MySqlCommand(query, dbCon.Connection); 

       cmd.ExecuteNonQuery(); 

      } 
      Server.Transfer("ReportBonds.aspx"); 
     } 

請幫助我......

+0

另外,您應該考慮自動實施的屬性。你的代碼可以*,很多*更短,功能沒有變化。 –

+0

你可以幫忙嗎?請使用 –

+0

什麼,使用自動實現的屬性?只需搜索以找出有關它們的信息,它應該變得明顯。 –

回答

2

基本上,問題是你是如何傳遞參數到數據庫中。你不應該需要自己創建一個MySqlDateTime - 只需使用參數化的SQL,它應該是罰款:

// TODO: Use a date/time control instead of parsing text to start with 
DateTime fdate = DateTime.Parse(TextBox3.Text); 
DateTime sdate = DateTime.Parse(TextBox4.Text); 

string query = @"INSERT INTO bondlocal (cancode,canname,bonddate) 
     SELECT t_bondridapp.cancode,t_bondridapp.canname,t_bondridapp.debissuedate 
     FROM t_bondridapp 
     WHERE debissuedate >= @fdate AND debissuedate <= @sdate"; 
using (var command = new MySqlCommand(query, dbCon)) 
{ 
    command.Parameters.Add("@fdate", MySqlDbType.Datetime).Value = fdate; 
    command.Parameters.Add("@sdate", MySqlDbType.Datetime).Value = sdate; 
    command.ExecuteNonQuery(); 
} 

基本上,你應該只使用字符串連接SQL內從未特定值。參數化SQL可防止SQL注入攻擊和轉換問題,並提高代碼的可讀性。

(順便說一句,我會勸你拋棄你的當前連接共享,而不是總是創建並打開一個新的MySqlDbConnection,並在你的操作結束時將其處理 - 依靠連接池,使其高效)。

+0

非常感謝您的先生。讓我試試 –

+0

先生,但是在「使用(var command = new MySqlCommand(query,conn))」語句中,「conn」是什麼意思。「 –

+0

@SachithParameswaran:數據庫連接。 'conn'是引用數據庫連接的變量比'dbCon'更常見的名稱,但我已經修改了答案。 –