使用命令对象更新Access DB中的行不起作用

时间:2016-04-03 05:29:46

标签: c# command

使用我的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();
            }
        }

1 个答案:

答案 0 :(得分:0)

您可能需要这样的内容,因为名称是保留字:

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