shamim shamim - 2 months ago 9
C# Question

In SQL Query how to set table name

I use C#. I fill a dataset with the result of sql query. Suppose

ds
is my dataset:

Dataset ds = new Dataset();
ds = GetTablesFromDataBase("Select * from Order, Select * from OrderDetails, Select * from Product");


After running the code above, my dataset contains three tables named
Table
,
Table1
,
Table2
. But I want my dataset table names to be the same as database tables names like:
Order
,
OrderDetails
,
Product
. Is there any way to write query or code, so that dataset table names become the same as database table names?

Please, don't propose the code below to change the table names of a dataset:

Dataset.TableName = "RequiredTableName";

Answer

You can use typed datasets or tablemapping

something like this

SqlDataAdapter da = new SqlDataAdapter(...);
DataSet ds = new DataSet();
DataTableMapping dtm1, dtm2, dtm3;
dtm1 = da.TableMappings.Add("Table", "Employees"); 
dtm2 = da.TableMappings.Add("Table1", "Products");
dtm3 = da.TableMappings.Add("Table2", "Orders");
da.Fill(ds);