开发者

Query MS Access database in VB 2008

I added an Access database as a Data Source in VB 2008. I want to query this databas开发者_JAVA百科e 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)


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜