Follow in Twitter & Facebook



Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools



Saturday, October 6, 2012

Conversion of DataReader to DataSet or DataTable and vice-versa

In this article we will discuss about how we can convert a data reader or dataset to data table or vice versa.

ADO.NET 2.0 allows loading a Data Reader object into a DataSet or a DataTable and vice versa. Both the Dataset and DataTable classes contains the Load method that can be used to load a DataReader instance into a DataSet or DataTable.

The following peaces of code shows how a DataTable can be loaded in a DataReader instance.

string connectionstring =...;//Some connection string

SqlConnection con = new SqlConnection(connectionstring);

con.Open();

SqlCommand cmd = new SqlCommand("Select * from Emp",con);

SqlDataReader dr = cmd.ExecuteReader(CommandBehaviour.CloseConnection);

DataTable dt = new DataTable("Employee");

dataTable.Load(dr);

The GetDataReader method of both the Dataset and DataTable classes can be used to retrive a DataReader instance from either a DataSet or a DataTable.If the DataSet instance on which the method is called contains multiple DataTable instances,the resultant DataReader would also contain multiple resulsets.




0 on: "Conversion of DataReader to DataSet or DataTable and vice-versa"