我试过下面的代码时,我要点击保存按钮我得到了错误“在命令执行过程中遇到致命错误”我重新检查两次以上,但不幸的是错误不要走开。请任何人善意修复这个错误。在命令执行过程中遇到致命错误在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();
}
}
什么是例外? –
而不是MessageBox.Show(ex.Message)MessageBox.Show(ex.ToString())或调试它并共享堆栈跟踪。 –