开发者

Unable to insert row to a table using OleDBAdapter

开发者 https://www.devze.com 2023-01-30 12:05 出处:网络
OleDbConnection _connection = new OleDbConnection(); StringBuilder ConnectionString = new StringBuilder(\"\");
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);
   开发者_JAVA技巧     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);

thats the code i use. as you can see i have a paradox table. and some how result = 0 at end of it all. any ideas what is my mistake?

thanks upfront.

-=Noam=-


What is your InsertCommand?

Also try after removing these line

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

if you call AcceptChanges all changes in the datatable is accepted so there is no rows which is changed so there is nothing to update


Remove call to AcceptChanges() :

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

According to MSDN:

Commits all the changes made to this DataSet since it was loaded or since the last time AcceptChanges was called.

Which means, it marks newly added row as not new.

0

精彩评论

暂无评论...
验证码 换一张
取 消