Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Import Access DB data into VB.Net Array

Status
Not open for further replies.

Sanderval

Technical User
Oct 6, 2004
7
0
0
US
I have several Access DB Queries that i would like to use to create arrays in a vb.net program. I believe i have the proper code to access the db, but i'm curious as to what method i would use to convert it to a array. Any one have ideas?

Here is the code i use to connect:
Code:
Dim ConnectionString As String _
        = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\pathtodata\database.mdb;User Id=admin;Password=;"
    Dim ObjCon As New OleDbConnection(ConnectionString)

Then I store the Access query commands as strings to run against the DB later:

Code:
Dim SampleQuery As String = "SELECT AccessTable.Field0, AccessTable.Field1, AccessTable.Field2,  FROM AccessTable;"

Then I build strings to execute commands against the DB:
Code:
Dim ExecuteQuery As New OleDbCommand(SampleQuery)
        ObjCon.Open()
        ExecuteQuery.ExecuteReader()


I'm not entirely sure that is the correct way to go about it.. but my limited vb.net knowledge pointed me in this direction, but i'm lost as to how i'd put the queried data into an array.
 
Hello I put together a few quick examples on how to read information from a microsoft access file. Example(1) shows how to put information into a dataset and then view that information. Example(2) shows how to load a combo box from column inside a microsoft access table. Hope this helps.

// example(1) How to load information into a dataset and
// view that information

'declare variables
Dim myquery As String
Dim myadapter As OleDbDataAdapter
Dim MyDatagrid As DataGrid
Dim mydataset As DataSet


'create query statement
myquery = "Select * from Letters"

Try
'open data connection
myconnection.Open()

'create an instance of oledbDataAdapter
myadapter = New OleDbDataAdapter(myquery, myconnection)

'create and instance of Dataset
mydataset = New DataSet

'link to the database and fill dataset
myadapter.Fill(mydataset, "Mytable")

'view information in a message box
MsgBox(mydataset.Tables(0).Rows(0).Item(0))
MsgBox(mydataset.Tables(0).Rows(1).Item(0))




Catch ex As Exception
Throw ex
Finally
'close connection
myconnection.Close()

End Try



// example(2) on how to load listbox items from an acces file

'declare variables
Dim mycommand As OleDbCommand
Dim myquery As String
Dim myreader As OleDbDataReader

'create query statement
myquery = "select [Letter Name] from Letters"

Try
'open database connection
myconnection.Open()

'create an instance of oledbcommand and set values
mycommand = New OleDbCommand(myquery, myconnection)



'grab info from database
myreader = mycommand.ExecuteReader()

'fill the combobox
While myreader.Read
cboLetters.Items.Add(myreader("Letter Name").ToString)
End While
Catch ex As Exception
Throw ex
Finally
'close connection
myconnection.Close()
'prepare for garbage collection
myquery = Nothing
mycommand = Nothing
myreader = Nothing
End Try
 
Thanks alot. That will be very helpful. If i can get it to work I'll post a simplified version of the end product.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top