Hi smc75, so i understand that there is no way to do that automatically from sqldatareader. How about the idea of Sarah in the email above. I didn t understand it. Did she mean there is a way to do all that code of Smc50 automatically. what is that Load method, is it a method of sqldatareader
The DataTable.Load method is new to .Net 2.0. It takes an IDataReader object and populates the DataTable object with it. This could not be done with any built in methods of the DataTable class in .Net 1.1. You can use the code provided to accomplish the same thing.
smc750
how can we put a resultset of a sqldatareader into a datatable object
how can we put a resultset of a sqldatareader into a datatable object
KirkHaselden
Hi smc75, so i understand that there is no way to do that automatically from sqldatareader. How about the idea of Sarah in the email above. I didn t understand it. Did she mean there is a way to do all that code of Smc50 automatically. what is that Load method, is it a method of sqldatareader
Thanks a lot
EvilMonkeySlayer
DataTable.Load in .NET 2.0 will do this.
Thanks,
Sarah
Chakry
RaminRow
Here is a variation of how you can do it in .Net 1.1
using System;
using System.Data;
namespace DataConversion
{
public class DataConverter
{
public DataConverter()
{}
public DataTable ReaderToTable(IDataReader dataReader,string tableName)
{
if( tableName.Length == 0)
tableName = "unknownTable";
DataTable schemaTable = null;
schemaTable = dataReader.GetSchemaTable();
DataTable fillTable = new DataTable(tableName);
DataRow fillRow;
string fieldName;
while( dataReader.Read() )
{
fillRow = fillTable.NewRow();
for( int fieldIdx = 0; fieldIdx < dataReader.FieldCount; fieldIdx++ )
{
fieldName = dataReader.GetName(fieldIdx);
if( fillTable.Columns.IndexOf(fieldName) == -1 )
{
Type typ = (Type) schemaTable.Rows[fieldIdx]["DataType"];
fillTable.Columns.Add( fieldName, typ );
}
fillRow[fieldName] = dataReader.GetValue(fieldIdx);
}
fillTable.Rows.Add(fillRow);
}
return fillTable;
}
}
}
make sure to close the dataReader when you are done
smc750
chialee
The DataTable.Load method is new to .Net 2.0. It takes an IDataReader object and populates the DataTable object with it. This could not be done with any built in methods of the DataTable class in .Net 1.1. You can use the code provided to accomplish the same thing.
smc750