Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL Query how to set table name

Tags:

c#

sql-server

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"; 
like image 789
shamim Avatar asked Mar 29 '26 19:03

shamim


1 Answers

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);
like image 51
Arsen Mkrtchyan Avatar answered Apr 02 '26 13:04

Arsen Mkrtchyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!