【问题标题】:Unable to insert row to a table using OleDBAdapter无法使用 OleDBAdapter 向表中插入行
【发布时间】:2011-05-24 14:32:30
【问题描述】:
OleDbConnection _connection = new OleDbConnection();
        StringBuilder ConnectionString = new StringBuilder("");
        ConnectionString.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;");
        ConnectionString.Append(@"Extended Properties=Paradox 5.x;");
        ConnectionString.Append(@"Data Source=C:\Clients\Rail\Wheelsets;");
        _connection.ConnectionString = ConnectionString.ToString(); 
        _connection.Open();
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Vehicles;", _connection);
        DataSet dsRetrievedData = new DataSet();
        da.Fill(dsRetrievedData);   
        OleDbCommandBuilder builder = new OleDbCommandBuilder(da);
        da.InsertCommand = builder.GetInsertCommand();
        ////Insert new row
        DataRow rowNew = dsRetrievedData.Tables[0].NewRow();
        rowNew[dsRetrievedData.Tables[0].Columns[0].ColumnName] = "978";
        rowNew[dsRetrievedData.Tables[0].Columns[1].ColumnName] = "222";
        rowNew[dsRetrievedData.Tables[0].Columns[4].ColumnName] = "999";
        rowNew[dsRetrievedData.Tables[0].Columns[5].ColumnName] = "999";
        rowNew[dsRetrievedData.Tables[0].Columns[6].ColumnName] = "999";
        dsRetrievedData.Tables[0].Rows.Add(rowNew);
        dsRetrievedData.Tables[0].AcceptChanges();
        dsRetrievedData.AcceptChanges();
        int result = da.Update(dsRetrievedData);

这就是我使用的代码。如您所见,我有一个悖论表。以及一些如何在这一切结束时结果 = 0。 任何想法我的错误是什么?

先谢谢了。

-=诺姆=-

【问题讨论】:

    标签: c# odbc paradox insert-into


    【解决方案1】:

    你的 InsertCommand 是什么?

    删除这些行后也试试

    dsRetrievedData.Tables[0].AcceptChanges();
    dsRetrievedData.AcceptChanges();
    

    如果您调用 AcceptChanges,则数据表中的所有更改都将被接受,因此没有任何行被更改,因此无需更新

    【讨论】:

    • 插入命令:INSERT INTO Vehicles(车辆编号、车辆制造、行驶距离、营业时间、卡车#1 序列号、卡车#2 序列号、卡车#3 序列号)值 (?, ?, ?, ?, ?, ?, ?) 删除这 2 行后,我得到一个语法异常。
    • INSERT INTO 语句中的语法错误。信息量不是很大。我之前问过,我得到了关于特殊字符的重播,因为悖论表需要用 ("") 引用,但是当我尝试使用 sql comman 时,我遇到了其他问题:stackoverflow.com/questions/4366988/…
    • Truck #1 序列号、Truck #2 序列号、Truck #3 序列号,这些列名称在您的文件中吗?
    • 不幸的是,这就是名称,我无法更改它们.....机器上还有其他应用程序使用此表。
    • 好的,尝试验证列名是问题,因此创建文件的副本并将这些列重命名为有意义的名称并使用此文件,然后尝试添加并验证文件中是否插入了行或者不通过打开文件并手动验证它。
    【解决方案2】:

    删除对AcceptChanges()的调用:

    dsRetrievedData.Tables[0].AcceptChanges();
    dsRetrievedData.AcceptChanges();
    

    According to MSDN:

    提交对此所做的所有更改 DataSet 自加载以来或自 上次 AcceptChanges 是 调用。

    也就是说,它将新添加的行标记为not new

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-21
      • 2021-12-07
      • 2022-10-24
      • 2013-10-21
      • 2022-12-05
      • 2014-07-03
      相关资源
      最近更新 更多