2017-06-21 66 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

我写了单独但错误没有去的方式 –

相关问题