Datatable equivalent code with OleDbDataReader

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I have below code which is written using a datatable dt. What is the
equivalent code using a OleDbDataReader?

For j = 0 To dt.Rows.Count - 1
Dim dataArr(dt.Columns.Count) As String
For i = 0 To dt.Columns.Count - 1
dataArr(i) = dt.Rows(j)(i)
Next
Next

Thanks

Regards
 
The equivalent would be (in my opinion):

Dim oddr As OleDb.OleDbDataReader
Dim odcmd As OleDb.OleDbCommand
Dim odconn As OleDb.OleDbConnection

odconn = New OleDb.OleDbConnection("connection string")
odcmd = New OleDb.OleDbCommand("command string", odconn)
oddr = odcmd.ExecuteReader

While (oddr.Read)
Dim dataArr(oddr.FieldCount) As String
For i = 0 To oddr.FieldCount - 1
dataArr(i) = oddr.GetValue(i).ToString()
Next
End While

I assume you just wanted to get a translation as the code doesn't keep the
values of dataArr beyond the inside of each loop.
 
The array declaration should be:
Dim dataArr(oddr.FieldCount-1) As String

Size specification in array declarations is the thing I dislike the
most about VB as it is probably my most repeated mistake. Lately I
have been trying to force myself to use the (0 To n) format to make me
remember what that it is the upper bound, not the count.
 
John,

It would be something

If you eat apples you cannot peal them in the same way as a banana

To get information from a datatable has nothing to do with get information
from a database, moreover, the fill command by instance uses a datareader to
fill the datatable.

Cor
 
Back
Top