I use a DataReader for all of
my base data access routines. Why? Pure speed,
and usually I
just want to read the data, so why not use the fastest
method
available? Now, sometimes I need a DataSet. No problem,
I've got a utility to
create one, and I just call that utility and pass it
my SqlDataReader. Voila!
A DataSet. Best of both worlds.
Only thing I don't deal with is complex sets
of tables within DataSets or
DataSets that update back to the server. I prefer
to write my own
update statements and put them in stored procedures, but that's
just
personal taste, I suppose.
You can see my DataReader to DataSet function here:
<begin code>
/// <summary>
/// Converts a SqlDataReader to a DataSet
/// <param name='reader'>
/// SqlDataReader to convert.</param>
/// <returns>
/// DataSet filled with the contents of the reader.</returns>
/// </summary>
public static DataSet convertReaderToGrid(SqlDataReader reader)
{
DataSet dataSet = new DataSet();
do
{
// Create new data table
DataTable schemaTable = reader.GetSchemaTable();
DataTable dataTable = new DataTable();
if ( schemaTable != null )
{
// A query returning records was executed
for ( int i = 0; i < schemaTable.Rows.Count; i++ )
{
DataRow dataRow = schemaTable.Rows[ i ];
// Create a column name that is unique in the data table
string columnName = ( string )dataRow[ "ColumnName" ]; //+ "<C" + i +
"/>";
// Add the column definition to the data table
DataColumn column = new DataColumn( columnName, ( Type )dataRow[
"DataType" ] );
dataTable.Columns.Add( column );
}
dataSet.Tables.Add( dataTable );
// Fill the data table we just created
while ( reader.Read() )
{
DataRow dataRow = dataTable.NewRow();
for ( int i = 0; i < reader.FieldCount; i++ )
dataRow[ i ] = reader.GetValue( i );
dataTable.Rows.Add( dataRow );
}
}
else
{
// No records were returned
DataColumn column = new DataColumn("RowsAffected");
dataTable.Columns.Add(column);
dataSet.Tables.Add( dataTable );
DataRow dataRow = dataTable.NewRow();
dataRow[0] = reader.RecordsAffected;
dataTable.Rows.Add( dataRow );
}
}
while ( reader.NextResult() );
return dataSet;
}
<end code>
Hallo Liste
Kennt jemand einen schnellen weg um die
Ergebnisse eines SqlDataReaders in ein DataSet umzuwandeln?
Gru�
Uwe Lyschik
-----------------------------------------------------------
Uwe
Lyschik
OMNITEK new media
Br�derstr. 26
59555 Lippstadt
Tel.
02941-247120
Fax.: 02941-247121
Web:
http:www.omnitek.de
-----------------------------------------------------------
|
[aspdedotnet] als [EMAIL PROTECTED] subscribed |
http://www.dotnetgerman.com/archiv/aspdedotnet/ = Listenarchiv | Sie k�nnen
sich unter folgender URL an- und abmelden: |
http://www.dotnetgerman.com/listen/aspDEdotnet.asp
|
[aspdedotnet] als [EMAIL PROTECTED] subscribed |
http://www.dotnetgerman.com/archiv/aspdedotnet/ = Listenarchiv | Sie k�nnen
sich unter folgender URL an- und abmelden: |
http://www.dotnetgerman.com/listen/aspDEdotnet.asp