---
title: "Convert a DataReader into a DataSet"
url: "https://osherove.com/blog/2004/1/22/convert-a-datareader-into-a-dataset.md"
canonical: "https://osherove.com/blog/2004/1/22/convert-a-datareader-into-a-dataset.html"
published: "2004-01-22T07:55:00"
updated: "2004-01-22T07:55:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "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…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Convert a DataReader into a DataSet

By Roy Osherove January 22, 2004 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published January 22, 2004; technical details may be outdated.

[Stefano describes a solution](http://dotnetjunkies.com/WebLog/demiliani/posts/5923.aspx) 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?

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Jan 23, 2004

    ### [Winform Singleton pattern example](https://osherove.com/blog/2004/1/23/winform-singleton-pattern-example.html)

- Jan 22, 2004

    ### [Israel DevDays](https://osherove.com/blog/2004/1/22/israel-devdays.html)

- Jan 20, 2004

    ### [Is SmallTalk a real option?](https://osherove.com/blog/2004/1/20/is-smalltalk-a-real-option.html)

- Jan 17, 2004

    ### [Learning from mistakes: the Don Quixote pattern](https://osherove.com/blog/2004/1/17/learning-from-mistakes-the-don-quixote-pattern-1.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
