vb.net - datareader to array

MDWinter
MDWinter used Ask the Experts™
on
Hi,

I am writing a vb.net application. The application pulls a query from a database via ODBC. I will want to look at the results of that query more than once as I compare various things against the values within it.

I figured I would create a two dimensional array and then populate each "row" of the array with the fifteen returned ordinals - and then I can run a for..next loop to iterate through the array as many times as I need.

But I can't seem to get my head aroudn arrays in vb.net.

Can someone please explain how I can create, populate and then read a two dimensional array?

I have put below the code which I have so far - which simply echoes each field of each row out on to the screen.

Thanks.

While dbreader.Read

            'strcol1 = dbreader.GetString(dbreader.GetOrdinal("cpd-desc"))
            'strcol2 = dbreader.GetString(dbreader.GetOrdinal("rate-region"))
            'strcol3 = dbreader.GetString(dbreader.GetOrdinal("rateid"))
            'Console.WriteLine(strcol1 & " - " & strcol2 & " - " & strcol3)


        End While

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
A dataset will meet your needs of reusing the data.


'define the dataset
dim ds as new dataset
ds.tables.add(new datatable)
with ds.tables(0).columns
	.add(new datacolumn("Ordinal"))
	.add(new datacolumn("Value"))
end with

dim row as datarow
'loop through the records form your reader adding a new row to the dataset
While dbreader.Read
	row=ds.tables(0).newrow
	row.item(0) = dbreader.item(0)
	row.item(1) = dbreader.item(1)
	ds.tables(0).rows.add(row)
End While
'always close your datareader when done
dbreader.close

Open in new window

Commented:
I do NOT recommend using arrays in VB.NET, because it is really slow compared to .NET's ArrayList,
but if you really want to use a two-dimensional array:
        Dim array(10, 15) As String 'you need to know the elements... redim preserve only works for last dimension
        Dim i, j As Integer

        'fill
        For i = array.GetLowerBound(0) To array.GetUpperBound(0)
            For j = array.GetLowerBound(1) To array.GetUpperBound(1)
                array(i, j) = i.ToString + " " + j.ToString()
            Next
        Next

        'read
        For i = array.GetLowerBound(0) To array.GetUpperBound(0)
            For j = array.GetLowerBound(1) To array.GetUpperBound(1)
                'RichTextBox1.Text += array(i, j) + Environment.NewLine
            Next
        Next

Open in new window

Author

Commented:
Thanks guys.

Not sure how I should award points here but no doubt a moderator can fix if required.

Looks like Pryrates gave the actual answer to my question - but Unified gave a more 'correct' way of doing it, so I've split the points. Both solutions work - I've tried them.

Hope that will be ok...

Commented:
it's ok to me ;-).
Like I mentioned array is not a good choice to use.
Prefer UnifiedIS solution or use List(Of String) constructs instead.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial