Rauf
Rauf

Reputation: 12852

Access tables in a DataSet using each table's name with C#

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

Upvotes: 1

Views: 1044

Answers (3)

to StackOverflow
to StackOverflow

Reputation: 124794

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);
...

Upvotes: 1

Tony Kh
Tony Kh

Reputation: 1572

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.

Upvotes: 0

Emond
Emond

Reputation: 50692

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?

Upvotes: 0

Related Questions