【问题标题】:Unhandled exception occurs when Clicking Save Button单击保存按钮时发生未处理的异常
【发布时间】:2020-09-19 21:29:25
【问题描述】:
private void btnSave_Click(object sender, EventArgs e)
{
    if (!ValidInput())
        return;
    using (var conn = new OleDbConnection(DatabaseObjects.ConnectionString))
    {
        conn.Open();
        command = new OleDbCommand("insert into Students (RollNo, SName, FName, DOB, Class, Section) values (@RollNo, @SName, @FName, @DOB, @Class, @Section)", conn);
        command.Parameters.Add(new OleDbParameter("RollNo", txtRollNo.Text));
        command.Parameters.Add(new OleDbParameter("SName", txtSName.Text));
        command.Parameters.Add(new OleDbParameter("FName", txtFName.Text));
        command.Parameters.Add(new OleDbParameter("DOB", dtpDOB.Text));
        command.Parameters.Add(new OleDbParameter("Class", cmbClass.Text));
        command.Parameters.Add(new OleDbParameter("Section", cmbSection.Text));
        command.ExecuteNonQuery();
        MessageBox.Show("Saved");
    }
}

当我点击保存按钮时,此错误正在上升(插入语法错误)。

我已经对代码进行了故障排除,我发现如果从 insert 语句中删除了字段 Section 和参数 @Section。然后代码工作正常。但我想获取它的 ComboBox(CmbSection) 值并存储在数据库中。我该怎么办?

【问题讨论】:

  • 你得到什么错误?
  • section 是 MS Access 保留字吗?试试[Section]
  • 我附上了错误图片。它给出(插入到语句语法错误)。单击保存按钮时。
  • 不知道“Section”是 MS-Access 中的保留字。

标签: c# sql database winforms ms-access


【解决方案1】:

“Section”在 MS Access 中是 a reserved word,所以在这里使用它会混淆查询解析器。

您可以通过将它们括在方括号中来明确告诉查询哪些标识符指的是数据库对象(列、表等):

INSERT INTO [Students]
    ([RollNo], [SName], [FName], [DOB], [Class], [Section])
    VALUES (@RollNo, @SName, @FName, @DOB, @Class, @Section)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    相关资源
    最近更新 更多