How can i retrieve the information from Ms Access and put it into a VB.net 2D Array? Urgently need help in this

Dani AI

Generated

Building on ’s idea to pull the table into an in-memory tabular object, here's a concrete, practical pattern for turning an Access table into a 2D VB.NET array. The example uses safe resource management, handles DBNull, and shows both a DataTable-based path (easy when the result size is known) and a DataReader path (lower memory overhead).

' DataTable -> 2D array (Object)
Dim connStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\path\to\file.accdb;"
Dim sql As String = "SELECT * FROM TableName"
Dim dt As New DataTable()
Using conn As New OleDbConnection(connStr)
    Using da As New OleDbDataAdapter(sql, conn)
        da.Fill(dt)
    End Using
End Using

If dt.Rows.Count = 0 Then Return

Dim rCount As Integer = dt.Rows.Count
Dim cCount As Integer = dt.Columns.Count
Dim arr(rCount - 1, cCount - 1) As Object

For r As Integer = 0 To rCount - 1
    For c As Integer = 0 To cCount - 1
        arr(r, c) = If(IsDBNull(dt.Rows(r)(c)), Nothing, dt.Rows(r)(c))
    Next
Next

Alternative (streaming) approach using OleDbDataReader and a temporary List(Of Object()) if the row count is unknown or the table is large. After reading, convert the list into a fixed 2D array the same way.

Practical notes and troubleshooting:

  • For older .mdb files the Jet provider is used; for .accdb use the ACE provider. On 64-bit machines, Jet requires x86 build or ACE 64-bit driver—set the project platform accordingly.
  • Use Object(,) if column types vary. Convert values with Convert.ToString or CType when specific types are required.
  • Watch memory for very large result sets; prefer the DataReader approach or process rows one-at-a-time.
  • A Dictionary is useful for key->value lookups, but not ideal if positional, row/column indexing is needed — arrays (or jagged arrays) suit that better.

This complements ’s suggestion with runnable patterns and common gotchas for Access + VB.NET scenarios that was asking about.

Recommended Answers

All 4 Replies

You can fetch data from DB and assign into dataset or datatable. Using the loop u can bind data into dictionary with key, value pair.

You can fetch data from DB and assign into dataset or datatable. Using the loop u can bind data into dictionary with key, value pair.

Can give me a sample program? my email is [email]<<Email Snipped.>>[/email]. Sorry i was very bad in this kind of programming

1.Specify database related stuff and fill the dataadapter into dataset
2. copy dataset to datatable
3.check the condition as datatable rows count
4.for loop i and for loop j (Nested loops)
5. DictionaryVari.Add("A","Apple");

1.Specify database related stuff and fill the dataadapter into dataset
2. copy dataset to datatable
3.check the condition as datatable rows count
4.for loop i and for loop j (Nested loops)
5. DictionaryVari.Add("A","Apple");

Ok i will try it, thanks for the answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.