1

Given an existing DataReader (in this case an OleDbDataReader), I can construct a DataTable using DataTable.Load:

// C#
OleDbDataReader dr = SomeMethodThatReturnsADataReader();
var dt = new DataTable();
dt.Load(dr);
' VB.NET
Dim dr As OleDbDataReader = SomeMethodThatReturnsADataReader();
Dim dt = New DataTable()
dt.Load(dr);

However, this loads the data from the DataReader into the DataTable.

I want to create a DataTable that matches the structure of an existing DataReader but doesn't contain any data.

How can I do this?

Edit

On further reflection, I understand there is no built-in method to do this. However, can the problem be broken down into two parts?

  1. Extract the schema information from the DataReader
  2. Load that schema information into a new DataTable
12
  • You may need to explain further - those DataReaders are just declared; we cant create DataReaders, they are always created for us. So where did yours actually come from? Commented Aug 25, 2016 at 0:41
  • @Plutonix The DataReader is actually created elsewhere; I've modified the code to clarify that. Commented Aug 25, 2016 at 1:00
  • I'd create another method to return a DataTable with the table def loaded - the DataReader isnt really meant for this. Given the way they can lock up a connection, it may not be a good idea to have a reader just laying around. Commented Aug 25, 2016 at 1:04
  • @Plutonix Not an issue. The actual code is an iterator function that returns a DataReader for each iteration, and wraps the yield return / Yield inside a using. I didn't want to include this, so as not to distract from my question. Commented Aug 25, 2016 at 1:11
  • 1
    There's no simple way to do it, i.e. no single method call. You'd have to call GetSchemaTable and then construct the schema of your DataTable based on that result. That's presumably what the DataTable.Load method does internally. Commented Aug 25, 2016 at 1:16

1 Answer 1

3

The DataAdapter Class, that is the base class of the OleDbDataAdapter and others, has the Protected FillSchema Method that takes an IDataReader to fill an empty DataTable's schema to match the reader's fields.

You need to construct a small utility class to make use of this feature.

Friend Class ReaderAdapter
    Inherits System.Data.Common.DataAdapter

    Public Function GetTypedTable(dataReader As IDataReader) As DataTable
        Dim ret As New DataTable
        FillSchema(ret, Data.SchemaType.Source, dataReader)
        Return ret
    End Function
End Class

Example use:

Dim rdr As OleDb.OleDbDataReader = cmd.ExecuteReader
Dim ra As New ReaderAdapter()
Dim emptyTypedTable As DataTable = ra.GetTypedTable(rdr)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.