gpt4 book ai didi

c# - 执行期间遇到 fatal error ...更新期间

转载 作者:行者123 更新时间:2023-11-29 06:39:26 25 4
gpt4 key购买 nike

此代码放在按钮中。当我单击它更新数据时,会出现一个消息框错误,提示“命令执行期间遇到 fatal error ”。

您的回答会很有帮助。谢谢

 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 = "";

最佳答案

参数 @SANCWhere 之间缺少空格。

试试这个:

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# - 执行期间遇到 fatal error ...更新期间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com