1
votes

A stored procedure returns multiple tables, the result set is assigned to a DataSet. Can I access the tables in the DataSet with each table's name?

For eg.:-

DataSet ds = Select(despatch_Packing_ID);

The DataSet contains 4 tables.

I am imposed to access the tables as

DataTable dtSales = ds.Tables[0];

How can I access the DataTable as

DataTable dtSales = ds.Tables["Sales"]; // Sales is tables where from I get data
3

3 Answers

1
votes

By default the table names generated by a DbDataAdapter will have the names "Table", "Table1", "Table2", ...

You can override this by specifying DataTableMappings.

For example:

DbDataAdapter adapter = ...
...
adapter.TableMappings.Add("Table", "Sales");
adapter.TableMappings.Add("Table1", "Customers");
...
adapter.Fill(myDataSet);
...
0
votes

The problem is that a query in a stored procedure can span multiple tables and views or even simply return a single value.

How would the DataTable get its name then?

0
votes

I suppose it doesn't work, because returned data may be produced from multiple joined tables. In such case there is no way to identify them by name. But I haven't checked myself, so it is just my guess.