问题
This code snippet is throwing an error:
Update unable to find TableMapping['Table'] or DataTable 'Table'.) on adapter.Update(ds); line
Why it is throwing this type of error?
SqlConnection con = new SqlConnection();
con.ConnectionString = connectionString();
DataSet ds = new DataSet();
string strQuery = "SELECT * FROM Cars";
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(strQuery, con);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Fill(ds, "Cars");
//Code to modify data in the DataSet
ds.Tables["Cars"].Rows[0]["Brand"] = "NewBrand";
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.Update(ds);
回答1:
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:
- It would be inefficient since it needs to loop all tables and all of their rows to find rows with a
RowState
!=Unchanged
- It's possible that multiple tables needs to be updated since they contain changed rows. That is not supported via
DataAdapter
. HenceDataAdapter.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.
回答2:
It's because .NET cannot assume that the table name in the DataSet
/DataTable
is identical to the database table. Thus .NET warns you about it.
To solve it you need to add a mapping to the DataAdapter
:
da.TableMappings.Add("TableNameInDb", "TableNameInTheDataSet");
However, even if you have specified a name in the DataSet
or the DataSource
it still doesn't work, since the adapter seems to forget the name.
I only got it working by using:
da.TableMappings.Add("Table", "TableNameInDb");
回答3:
I agree with jgauffin and I will only explain it with more text.
First, I must explain how TableMapping
works. If we don't specify TableMappings
with SqlDataAdapter
(SqlDataAdapter
that will fill our DataSet
) then by default the first table will be named "Table", the second will be named "Table1", the third will be named "Table2" etc.
So, when we want to name DataTable
in DataSet
, we use it like this:
System.Data.DataSet myDataSet = new System.Data.DataSet();
using (System.Data.SqlClient.SqlDataAdapter dbAdapter = new System.Data.SqlClient.SqlDataAdapter(dbCommand))
{
dbAdapter.TableMappings.Add("Table", "Cars");
dbAdapter.TableMappings.Add("Table1", "Trucks");
//...
dbAdapter.Fill(myDataSet);
}
And only then we can modify it like this:
myDataSet.Tables["Cars"].Rows[0]["Brand"] = "Toyota";
myDataSet.Tables["Trucks"].Rows[0]["Brand"] = "MAN";
来源:https://stackoverflow.com/questions/19228608/dataadapter-update-unable-to-find-tablemappingtable-or-datatable-table