执行期间遇到致命错误...更新期间 [英] fatal error encountered during execution... during update
问题描述
此代码放置在按钮中.当我单击它更新数据时,会出现一个消息框错误,提示在命令执行期间遇到致命错误".
This code is placed in the button. and when i click it to update the data, a messagebox error appears saying "fatal error encountered during command execution".
您的回答会很有帮助.谢谢
Your answers would be a great help. Thank you
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
try
{
connection.Open();
cmd = connection.CreateCommand();
cmd.CommandText = "UPDATE student_offense SET TYPE=@TYPE,DATE_HAPPENED=@DH,DESCRIPTION=@DESC,SANCTION=@SANC" +
"Where STUDENT_NO = @STUDENT_NO And DESCRIPTION=@DESC And SANCTION=@SANC And DATE_HAPPENED=@DH";
cmd.Parameters.AddWithValue("@TYPE", offense_combo.Text);
cmd.Parameters.AddWithValue("@DH", date_hapen.Text);
cmd.Parameters.AddWithValue("@DESC", description_txt.Text);
cmd.Parameters.AddWithValue("@SANC", sanction_txt.Text);
cmd.Parameters.AddWithValue("@STUDENT_NO", studentNo_txt.Text);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
MessageBox.Show("updated");
//refresh
cmd.CommandText = "SELECT student_info.Student_no,student_info.Lastname,student_info.Firstname,student_offense.Type,student_offense.Description,student_offense.Date_Happened,student_offense.Sanction,student_offense.Date_Recorded from student_info,student_offense where student_info.student_no = student_offense.student_no";
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
dbdataset = new DataTable();
sda.Fill(dbdataset);
bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
student_no_valid.Visible = false;
stud_no_error.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
listBox1.Items.Clear();
description_txt.Text = "";
studentNo_txt.Text = "";
offense_combo.Text = "";
current_date();
sanction_txt.Text = "";
推荐答案
您在参数 @SANC
和 Where
之间缺少空格.
You are missing space between Parameter @SANC
and Where
.
试试这个:
cmd.CommandText = "UPDATE student_offense SET TYPE=@TYPE,DATE_HAPPENED=@DH,
DESCRIPTION=@DESC,SANCTION=@SANC" + " Where STUDENT_NO = @STUDENT_NO And
DESCRIPTION=@DESC And SANCTION=@SANC And DATE_HAPPENED=@DH";
建议:如果您的 DATE_HAPPENED
列类型在您的表中是 Date
,那么您需要发送正确的 Date
代码> 格式.
Suggestion : if your DATE_HAPPENED
column type is Date
in your table,then You need to send the proper Date
format.
试试这个:假设用户以 dd-MM-yyyy
格式输入日期.
Try This: Assuming user enters Date in dd-MM-yyyy
format.
DateTime dt = DateTime.ParseExact(date_hapen.Text,"dd-MM-yyyy",
CutureInfo.InvariantCulture);
现在在分配 DATE_HAPPENED
值时提供以下格式
Now while assigning the DATE_HAPPENED
value provide the following format
cmd.Parameters.AddWithValue("@DH",dt.ToString("yyyy-MM-dd"));
这篇关于执行期间遇到致命错误...更新期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!