How to save a retrieved column from a datareader into an array?
I would like to retrieve a single column of about 800 rows in a table and save these values in an array so as to reference t开发者_开发问答hem later on. How can I do it? I tried:
con = New OleDb.OleDbConnection("provider=SQLOLEDB;data source=PC;initial catalog=DB1;integrated security=SSPI")
cmd = New OleDbCommand("select col1 from table1", con)
con.Open()
r = cmd.ExecuteReader
While r.Read
prev_ob(i) = r.Item(0)(0)
i = i + 1
End While
For i = 0 To UBound(prev_ob)
Console.WriteLine(prev_ob(i))
Next`
And it didn't work. The col1 is of type Int64 and so is the array prev_ob(). Please let me know how I can save it to an array.
What is the error your are getting? More information will help you in getting an answer sooner.
Try this:
Dim int64list As New List(Of Int64)
cmd = New OleDbCommand("select col1 from table1", con)
con.Open()
r = cmd.ExecuteReader
While r.Read
int64list.Add(Convert.ToInt64(r.Item(0))
End While
For Each obj In int64list
Console.WriteLine(obj)
Next obj
PS I wrote a sample that works in C# and tried translating to VB.net.
精彩评论