Example of How to convert DataReader to dataset in C#?

Hi all I am a new member of this forum .This is the code to convert a DataReader into Dataset in C#.



public DataSet ConvertDataReaderToDataSet(System.Data.SqlClient.SqlDataReader reader)

{

DataSet dataSet = new DataSet();

DataTable schemaTable = reader.GetSchemaTable();

DataTable dataTable = new DataTable();

for (int i = 0; i <= schemaTable.Rows.Count - 1; i++)

{

DataRow dataRow = schemaTable.Rows+;

string columnName = dataRow["ColumnName"].ToString();

DataColumn column = new DataColumn(columnName, dataRow["DataType"].GetType());

dataTable.Columns.Add(column);

}

dataSet.Tables.Add(dataTable);

while (reader.Read())

// http://www.infysolutions.com

{

DataRow dataRow = dataTable.NewRow();

for (int i = 0; i <= reader.FieldCount - 1; i++)

{

dataRow+ = reader.GetValue(i);

}

dataTable.Rows.Add(dataRow);

}

return dataSet;

}

With Best Wishes
-



Software Development Company

Edited by: techexpert on Jul 17, 2008 11:27 AM

Please sign in to leave a comment.