Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store multiple DataTables into single DataSet in c#?

I have the code below which has multiple DataTables with results and I need to pass a single DataSet with all DataTable values.

MySqlCommand cmd = new MySqlCommand(getLikeInfo, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

MySqlCommand cmd1 = new MySqlCommand(getKnowInfo, con);
MySqlDataAdapter da1 = new MySqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);

MySqlCommand cmd2 = new MySqlCommand(getHotelInfo, con);
MySqlDataAdapter da2 = new MySqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);

MySqlCommand cmd3 = new MySqlCommand(getRoomInfo, con);
MySqlDataAdapter da3 = new MySqlDataAdapter(cmd3);
DataTable dt3 = new DataTable();
da3.Fill(dt3);

MySqlCommand cmd4 = new MySqlCommand(getFoodInfo, con);
MySqlDataAdapter da4 = new MySqlDataAdapter(cmd4);
DataTable dt4 = new DataTable();
da4.Fill(dt4);

How can I do that?

like image 232
Heena Avatar asked Oct 03 '11 05:10

Heena


People also ask

How to add Multiple tables in DataSet?

The SqlDataAdapter object allows us to populate DataTables in a DataSet. We can use Fill method in the SqlDataAdapter for populating data in a Dataset. We can populate Dataset with more than one table at a time using SqlDataAdapter Object .

How to add Multiple DataSet in one DataSet in c#?

For this we can use the Merge() method in C#. Rules to display two DataSets in a GridView: All the columns specified in the datagrid must be present in both datasets. The data type of all columns in the datasets must be the same.


1 Answers

DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
...

If you want your tables named in the DataSet you can create your tables from the DataSet

DataSet ds = new DataSet();
DataTable t1 = ds.Tables.Add("t1");  // Create
DataTable t1Again = ds.Tables["t1"]; // fetch by name

You can also set the TableName property of the tables if you use the first approach.

like image 185
Albin Sunnanbo Avatar answered Sep 22 '22 10:09

Albin Sunnanbo