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?
- Extract the schema information from the
DataReader - Load that schema information into a new
DataTable
DataReaderis actually created elsewhere; I've modified the code to clarify that.DataReaderfor each iteration, and wraps theyield return/Yieldinside ausing. I didn't want to include this, so as not to distract from my question.GetSchemaTableand then construct the schema of yourDataTablebased on that result. That's presumably what theDataTable.Loadmethod does internally.