Logan Serman
Logan Serman

Reputation: 29880

Query MS Access database in VB 2008

I added an Access database as a Data Source in VB 2008. I want to query this database and use the information in various ways throughout the program. For example, there is an Employee table with first/last names of employees. I have a combobox on my form that I want to display all of the employees. So I want to query the database for all the rows in the Employee table, and add them to the combobox as I go.

I am familiar with SQL Syntax, so I am not asking how to write the query itself, but rather how to fetch rows in VB code (mimicking php's mysql_fetch_assoc and mysql_connect essentially)

Thanks!

Edit: Also, I want to know if I can query a DB if I don't add it as a data source (if I know the path name of the database)

Upvotes: 3

Views: 2428

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416111

You use the classes in the System.Data.OleDb namespace to query access databases:

Using cn As New OleDbConnection("connection string here"), _
      cmd As New OleDbCommand("SELECT query with ? parameter here", cn)

    cmd.Parameters.Add("?", OleDbType.Int).Value = 1234

    MyCombobox.DataSource = cmd.ExecuteReader()
End Using

Upvotes: 4

Related Questions