此代码位于按钮中。当我单击它更新数据时,会出现一个消息框错误,显示“在命令执行期间遇到致命错误”。
你的回答会很有帮助的。谢谢你
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
之间缺少空格。
试试这个:
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
格式。试试这个:假设用户以
dd-MM-yyyy
格式输入日期。DateTime dt = DateTime.ParseExact(date_hapen.Text,"dd-MM-yyyy",
CutureInfo.InvariantCulture);
现在,在分配
DATE_HAPPENED
值时,提供以下格式cmd.Parameters.AddWithValue("@DH",dt.ToString("yyyy-MM-dd"));
关于c# - 执行期间遇到致命错误…更新期间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22440605/