DataAdapter: Update unable to find TableMapping['Table'] or DataTable 'Table'

前端 未结 3 1273
渐次进展
渐次进展 2021-01-17 23:52

This code snippet is throwing an error:

Update unable to find TableMapping[\'Table\'] or DataTable \'Table\'.) on adapter.Update(ds); line

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-18 00:03

    Use

    adapter.Update(ds, "Cars");
    

    instead.

    I have tested it. I got the same error without and it works if i specify the tablename. However, i must admit that i yet don't know why the DataAdapter needs to know the table-name since it has all informations it needs. If i useds.GetChanges i get one row with the correct table-name.

    Update I have found nothing on MSDN but finally found it in the source(ILSpy). Here is the implementation of DBDataAdapter.Update(DataSet):

    public override int Update(DataSet dataSet)
    {
        return this.Update(dataSet, "Table");
    }
    

    So if you don't specify a table, the table-name "Table" is used and if you've specified a table-name other than that you'll get this error, that's really strange!

    I assume that the reason for this is that the DataAdapter cannot call GetChanges to determine the table to update for two reasons:

    1. It would be inefficient since it needs to loop all tables and all of their rows to find rows with a RowState != Unchanged
    2. It's possible that multiple tables needs to be updated since they contain changed rows. That is not supported via DataAdapter. Hence DataAdapter.Update(DataSet) assumes the default name "Table" as table-name.

    Edit: However, maybe someone can explain me why the DataAdapter doesn't use DataSet.Tables[0].TableName instead.

    So in general it seems to be best practise to specify the name of the table you want to update.

提交回复
热议问题