2017-06-21 48 views
0

我試過下面的代碼時,我要點擊保存按鈕我得到了錯誤「在命令執行過程中遇到致命錯誤」我重新檢查兩次以上,但不幸的是錯誤不要走開。請任何人善意修復這個錯誤。在命令執行過程中遇到致命錯誤在c#.net mysql

private void button1_Click(object sender, EventArgs e) 
     { 
      string cid, lname, fname,street,city,state,phone,date,email,aco,actype,des,bal; 

      cid = label14.Text; 
      lname = textBox1.Text; 
      fname = textBox2.Text; 
      street = textBox3.Text; 
      city = textBox4.Text; 
      state = textBox5.Text; 
      phone = textBox6.Text; 
      date = dateTimePicker1.Text; 
      email = textBox8.Text; 


      aco = textBox7.Text; 
      actype = comboBox1.Text; 
      des = textBox10.Text; 
      bal = textBox11.Text; 

      con.Open(); 


      MySqlCommand cmd = con.CreateCommand(); 
      MySqlTransaction transaction; 

      transaction = con.BeginTransaction(); 

      StringBuilder cmdText = new StringBuilder(); 
      cmdText.AppendLine("INSERT into customer (custid,lastname,firstname,street,city,state,phone,date,email) VALUES (@custid,@lastname,@firstname,@street,@city,@state,@phone,@date,@email)"); 
      cmdText.AppendLine("INSERT into account(accid,custid,acctype,description,balance) VALUES (@accid,@custoid,@acctype,@description,@balance)"); 



      cmd.CommandText = cmdText.ToString(); 
      cmd.Connection = con; 
      cmd.Transaction = transaction; 

      cmd.Parameters.AddWithValue("@custid", cid); 
      cmd.Parameters.AddWithValue("@lastname", lname); 
      cmd.Parameters.AddWithValue("@firstname", fname); 
      cmd.Parameters.AddWithValue("@street", street); 
      cmd.Parameters.AddWithValue("@city", city); 
      cmd.Parameters.AddWithValue("@state", state); 
      cmd.Parameters.AddWithValue("@phone", phone); 
      cmd.Parameters.AddWithValue("@date", date); 
      cmd.Parameters.AddWithValue("@email", email); 

      cmd.Parameters.AddWithValue("@accid", aco); 
      cmd.Parameters.AddWithValue("@cusotid", cid); 
      cmd.Parameters.AddWithValue("@acctype", actype); 
      cmd.Parameters.AddWithValue("@description", des); 
      cmd.Parameters.AddWithValue("@balance", bal); 







      try 
      { 
       cmd.ExecuteNonQuery(); 



       transaction.Commit(); 
       MessageBox.Show("Transaction Suceess"); 
      } 
      catch (Exception ex) 
      { 
       transaction.Rollback(); 
       MessageBox.Show(ex.Message); 
      } 
      finally 
      { 
       con.Close(); 
      } 

     } 
+0

什麼是例外? –

+0

而不是MessageBox.Show(ex.Message)MessageBox.Show(ex.ToString())或調試它並共享堆棧跟蹤。 –

回答

0

我見過很多開發人員,因爲他們對自己的SqlCommand使用AddWithValue遇到與他們的SQL錯誤。這個問題是該命令不知道你的sql命令參數的數據類型。

您應該使用SqlParameterCollection.Add Method (String, SqlDbType, Int32)來指定參數的數據類型。有關枚舉SqlDbType,請參閱SqlDbType Enumeration

用法:

cmd.Parameters.Add("@custid", SqlDbType.Int).Value = cid; 
cmd.Parameters.Add("@lastname", SqlDbType.Text).Value = lname; 

附:我假設你的SQL連接字符串沒有問題。

+0

cmd.Parameters.AddWithValue(「@ custid」,MySqlDbType.Int32).Value = cid; cmd.Parameters.AddWithValue(「@ lastname」,MySqlDbType.Text).Value = lname; cmd.Parameters.AddWithValue(「@ firstname」,MySqlDbType.Text).Value = fname; 但agian我得到了1錯誤輸入字符串不是當前格式 –

+0

@PrabigaDuraisamy嘗試'@ custid'上的'SqlDbType.String'。我還建議使用單獨的'SqlCommands'來插入'customer'和'account'表。 –

+0

我寫了單獨但錯誤沒有去的方式 –

相關問題