shamim shamim - 1 year ago 74
C# Question

In SQL Query how to set table name

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

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
. But I want my dataset table names to be the same as database tables names like:
. 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 Source

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");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download