Peter Osawa said:
The problem es that I receive the IDataReader and I'm not allowed to
change that...
The following will convert the Reader to a DataTable. I don't know how to do
this using any methods on any of the Sql objects, because I haven't spent much
time with the Reader specifically. So I wrote this quickly to help you in your
situation.
Hope it helps!
Mythran
' --------------------------------------------------------------------------
' Description:
' Sample MAIN() console application.
'
Sub Main()
Dim conn As SqlConnection
Dim read As SqlDataReader
Dim adap As SqlDataAdapter
Dim dt As DataTable
Dim cmd As SqlCommand
conn = New
SqlConnection("Server=MyServer;Database=MyDatabase;Trusted_Connection=True")
conn.Open()
cmd = New SqlCommand("SELECT * FROM tblDepartment", conn)
read = cmd.ExecuteReader()
' Convert the reader to a data table.
dt = ConvertReaderToDataTable(read)
' Close the reader and the database connection.
read.Close()
conn.Close()
' Write the first row of the data table.
For Each col As DataColumn In dt.Columns
Console.WriteLine("Name: " & col.ColumnName & _
" - Value: " & dt.Rows(0)(col).ToString())
Next
End Sub
' --------------------------------------------------------------------------
' Description:
' Convert the reader to a data table.
'
Public Function ConvertReaderToDataTable(ByVal Reader As SqlDataReader) As
DataTable
Dim dt As DataTable
Dim dr As DataRow
' Create the data table.
dt = New DataTable()
' Add the columns from the reader to the schema of the data table.
For i As Integer = 0 To Reader.FieldCount - 1
dt.Columns.Add(Reader.GetName(i), Reader.GetFieldType(i))
Next
' Add each row in the reader to the data table.
While Reader.Read()
' Create the new row.
dr = dt.NewRow()
For i As Integer = 0 To Reader.FieldCount - 1
dr(i) = Reader(i)
Next
' Add the row to the table.
dt.Rows.Add(dr)
End While
' Return the DataTable object.
Return dt
End Function