在 VB 2008 中查询 MS Access 数据库 [英] Query MS Access database in VB 2008
问题描述
我在 VB 2008 中添加了一个 Access 数据库作为数据源.我想查询这个数据库并在整个程序中以各种方式使用这些信息.例如,有一个员工表,其中包含员工的名字/姓氏.我的表单上有一个组合框,我想显示所有员工.所以我想在数据库中查询 Employee 表中的所有行,然后将它们添加到组合框中.
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.
我熟悉 SQL 语法,所以我不是问如何编写查询本身,而是如何在 VB 代码中获取行(本质上模仿 php 的 mysql_fetch_assoc 和 mysql_connect)
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)
谢谢!
另外,我想知道如果我不将其添加为数据源(如果我知道数据库的路径名)是否可以查询数据库
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)
推荐答案
您使用 System.Data.OleDb
命名空间中的类来查询访问数据库:
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
这篇关于在 VB 2008 中查询 MS Access 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!