0
    void updateTaxPayer()
    {
        DataRow[] ssnRow = dbSet.Tables["TrackingSheet"].Select("SSN = '" + ssn + "'");
        ssnRow[0]["HAS SPOUSE"] = checkboxToString(chkSpouse.CheckState);
        ssnRow[0]["SPOUSE SSN"] = txtSpouseSSN.Text;
        ssnRow[0]["TNAME"] = txtName.Text;
        ssnRow[0]["ADDRESS"] = txtStreetAddress.Text;
        ssnRow[0]["CITY"] = txtCity.Text;
        ssnRow[0]["STATE"] = cbxState.SelectedIndex.ToString();
        ssnRow[0]["ZIP"] = txtZip.Text;
        ssnRow[0]["HAS NEW ADDRESS"] = checkboxToString(chkNewAddress.CheckState);
        ssnRow[0]["NEW ADDRESS"] = txtNewAddress.Text;
        ssnRow[0]["NEW CITY"] = txtNewCity.Text;
        ssnRow[0]["NEW STATE"] = cbxNewState.SelectedIndex.ToString();
        ssnRow[0]["NEW ZIP"] = txtNewZip.Text;
        ssnRow[0]["DATE FILED"] = txtDate.Text;
        ssnRow[0]["SSN"] = txtSSN.Text;
        dbAdapt.InsertCommand = cmdBuilder.GetInsertCommand(true);
        dbAdapt.UpdateCommand = cmdBuilder.GetUpdateCommand(true);
        dbAdapt.DeleteCommand = cmdBuilder.GetDeleteCommand(true);
        dbAdapt.Update(dbSet,"TrackingSheet");

    }

以上是我必须更新数据库的代码,但由于某种原因,当我尝试更新时,我收到更新命令的语法错误。我四处寻找,我不知道该怎么做。

我也试过 dbAdapt.Update(dbSet, "TrackingSheet"); 那也没有用。

确切的错误是:

System.Data.OleDb.OleDbException (0x80040E14): Syntax error in UPDATE statement.
   at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
   at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
   at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
   at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
   at trackingform.frmTrackingSheet.updateTaxPayer() in c:\Users\Anthony\Documents\Visual Studio 2012\Projects\trackingform\trackingform\frmTrackingSheet.cs:line 595
   at trackingform.frmTrackingSheet.btnSave_Click(Object sender, EventArgs e) in c:\Users\Anthony\Documents\Visual Studio 2012\Projects\trackingform\trackingform\frmTrackingSheet.cs:line 506

第 595 行:dbAdapt.Update(dbSet,"TrackingSheet"); 第 506 行:updateTaxPayer();

其他声明:

dbAdapt = new OleDbDataAdapter();
cmdBuilder = new OleDbCommandBuilder(dbAdapt);
dbAdapt.SelectCommand = dbCmd;
dbSet = new DataSet("TrackingSheet");
dbAdapt.Fill(dbSet, "TrackingSheet");

有人有想法么?谢谢

4

1 回答 1

0

不能肯定地说,但我怀疑这与您命名列的方式有关 - 使用包含空格的列名,并在“DATE”等列名的一部分之间为解析器创建一些歧义和保留字或函数名称(如“DATE”函数)。如果您要手动编写语句,则需要将这些列名括在引号中,以便 SQL 解析器正确理解它们,但命令生成器可能不会这样做,因此会产生无效的 SQL。

作为一般做法,我避免在列名中使用空格,并尽量避免使用“保留字”......因为它永远无法确定什么会或不会起作用。

于 2013-06-17T20:55:47.143 回答