The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Convert a DataReader into a DataSet

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
Convert a DataReader into a DataSet Posted: Jan 22, 2004 4:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: Convert a DataReader into a DataSet
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement

Stefano describes a solution they use in a webservice, needing to convert a data reader to a dataset:

 

Public Function ConvertDataReaderToDataSet(ByVal reader As SqlDataReader) As DataSet

Dim dataSet As DataSet = New DataSet()

Dim schemaTable As DataTable = reader.GetSchemaTable()

Dim dataTable As DataTable = New DataTable()

Dim intCounter As Integer

For intCounter = 0 To schemaTable.Rows.Count - 1

Dim dataRow As DataRow = schemaTable.Rows(intCounter)

Dim columnName As String = CType(dataRow("ColumnName"), String)

Dim column As DataColumn = New DataColumn(columnName, _

CType(dataRow("DataType"), Type))

dataTable.Columns.Add(column)

Next

dataSet.Tables.Add(dataTable)

While reader.Read()

Dim dataRow As DataRow = dataTable.NewRow()

For intCounter = 0 To reader.FieldCount - 1

dataRow(intCounter) = reader.GetValue(intCounter)

Next

dataTable.Rows.Add(dataRow)

End While

Return dataSet

End Function

 

While I can see the benefit in this, it seems as though this can create very high perf issues. GetSchemaTable is another round trip to the database and its a costly one. Create a DataTable on the file is also kinda costly. A better solution might be to incorporate a sort of caching mechanism. Remember SqlHelper from the data application block?  It uses a caching mechanism for sproc parameter arrays so that sproc params are "discovered" only once and saved for later use. Same thing could be done here. Caching an array of columns should be done using some sort of key, but, what key can be used here? the reader has no "commandText" property that can be used. What do you think?

Read: Convert a DataReader into a DataSet

Topic: Integration Techniques and Strategies for Content Management Server 2002 online book Previous Topic   Next Topic Topic: Short but sweet little example from today's .NET Wire

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use