【问题标题】:Updating row in Access DB using command object is not working使用命令对象更新 Access DB 中的行不起作用
【发布时间】:2016-04-03 05:29:46
【问题描述】:

使用我的 Windows 窗体应用程序中的命令对象更新访问数据库中的一行的简单任务陷入了困境。我能够插入记录,但不知何故无法更新记录:

 private void openDB()
        {
            DBPath = Application.StartupPath + "\\myDB.mdb";
            conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source=" + DBPath);
            conn.Open();
        }

 private void btnUpdate_Click(object sender, EventArgs e)
        {
            string insertString;
            openDB();
            string updateString;
            updateString = "Update Address SET Name='"+ txtName.Text.Trim() +  "', IsActive='" + chkAddressActive.Checked +"' where MemberID="+txtMemberID.Text.Trim();
            //MessageBox.Show(updateString);
            using (OleDbCommand updateCmd = new OleDbCommand(updateString, conn))
            {
                updateCmd.ExecuteNonQuery();
                MessageBox.Show("Record Updated Successfully", "Transaction", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dgView.Enabled = true;
                ReloadDataForSelectedMember();
            }
        }

【问题讨论】:

  • 您能否发布一个生成的查询字符串的示例

标签: c# command


【解决方案1】:

你可能需要这样的东西,因为 Name 是一个保留字:

updateString = "Update Address SET [Name] = '" + txtName.Text.Trim() + "', IsActive = " + chkAddressActive.Checked.ToString() + " Where MemberID = " + txtMemberID.Text.Trim();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 2011-06-03
    相关资源
    最近更新 更多