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

前端 未结 3 1282
渐次进展
渐次进展 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:10

    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";
    

提交回复
热议问题