How to Copy a datareader to a dataset
private DataSet CopyReaderToDataSet(SqlDataReader reader)
{
DataSet dataSet = new DataSet();
System.Data.DataTable schemaTable = reader.GetSchemaTable();
System.Data.DataTable dataTable = new System.Data.DataTable();
for (int cntr = 0; cntr < schemaTable.Rows.Count; ++cntr)
{
DataRow dataRow = schemaTable.Rows[cntr];
string columnName = dataRow["ColumnName"].ToString();
Type colType = dataRow["ColumnName"].GetType();
DataColumn column = new DataColumn(columnName, colType);
dataTable.Columns.Add(column);
}
dataSet.Tables.Add(dataTable);
while (reader.Read())
{
DataRow dataRow = dataTable.NewRow();
for (int cntr = 0; cntr < reader.FieldCount; ++cntr)
{
dataRow[cntr] = reader.GetValue(cntr);
}
dataSet.Tables[dataTable.TableName.ToString()].Rows.Add(dataRow);
}
return dataSet;
}
Reference: Roni's blog, added couple of lines missing in his blog
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment