Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill Dataset with multiple tables?

Tags:

I'm trying to fill DataSet which contains 2 tables with one to many relationship. I'm using DataReader to achieve this :

    public DataSet SelectOne(int id)     {         DataSet result = new DataSet();         using (DbCommand command = Connection.CreateCommand())         {             command.CommandText = "select * from table1";              var param = ParametersBuilder.CreateByKey(command, "ID", id, null);             command.Parameters.Add(param);              Connection.Open();             using (DbDataReader reader = command.ExecuteReader())             {                 result.MainTable.Load(reader);             }             Connection.Close();         }         return result;     } 

But I've got only one table filled up. How do I achieve my goal - fill both tables?

I would like to use DataReader instead DataAdapter, if it possible.

like image 628
Andriy Zakharko Avatar asked Jul 05 '12 13:07

Andriy Zakharko


People also ask

Can we load multiple tables in a DataSet?

Overview. In the Etlworks Integrator, it is possible to read data from one dataset and load it into multiple database tables.

How do I add multiple tables in one DataSet?

The DataTableCollection contains zero or more DataTable objects. 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 do you fill data in a DataSet?

The Fill method of the DataAdapter is used to populate a DataSet with the results of the SelectCommand of the DataAdapter . Fill takes as its arguments a DataSet to be populated, and a DataTable object, or the name of the DataTable to be filled with the rows returned from the SelectCommand .

How many tables a DataSet can contain?

The Google Cloud console can display up to 50,000 tables for each dataset.


2 Answers

Filling a DataSet with multiple tables can be done by sending multiple requests to the database, or in a faster way: Multiple SELECT statements can be sent to the database server in a single request. The problem here is that the tables generated from the queries have automatic names Table and Table1. However, the generated table names can be mapped to names that should be used in the DataSet.

SqlDataAdapter adapter = new SqlDataAdapter(       "SELECT * FROM Customers; SELECT * FROM Orders", connection); adapter.TableMappings.Add("Table", "Customer"); adapter.TableMappings.Add("Table1", "Order");  adapter.Fill(ds); 
like image 144
Mohsen Safari Avatar answered Sep 20 '22 14:09

Mohsen Safari


If you are issuing a single command with several select statements, you might use NextResult method to move to next resultset within the datareader: http://msdn.microsoft.com/en-us/library/system.data.idatareader.nextresult.aspx

I show how it could look bellow:

public DataSet SelectOne(int id) {     DataSet result = new DataSet();     using (DbCommand command = Connection.CreateCommand())     {         command.CommandText = @" select * from table1 select * from table2         ";          var param = ParametersBuilder.CreateByKey(command, "ID", id, null);         command.Parameters.Add(param);          Connection.Open();         using (DbDataReader reader = command.ExecuteReader())         {             result.MainTable.Load(reader);             reader.NextResult();             result.SecondTable.Load(reader);             // ...         }         Connection.Close();     }     return result; } 
like image 36
smoothdeveloper Avatar answered Sep 21 '22 14:09

smoothdeveloper