vb.net - 传递参数以查询 Access 数据库

标签 vb.net ms-access

我正在使用以下代码并尝试通过给定参数获取数据。我不知道如何将参数值传递给我的查询。

Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String

con.ConnectionString = "
                 PROVIDER=Microsoft.Jet.OLEDB.4.0;
                 Data Source = D:\.Net Programs\DB Experiments\AddressBook.mdb"

        con.Open()
        sql = "SELECT * FROM tblContacts where Name=? and City=?"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AddressBook")
        con.Close()

最佳答案

我会首先创建一个 OleDbCommand 对象并使用该对象创建一个 OleDbDataAdapter

Imports Data.OleDb

dim cmd as new OleDbCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT * FROM tblContacts where Name=? and City=?"

' Here we add the parameters in the same order they appear in the
' CommandText. The Name of the paramters can be anything when using
' a Jet database, only the order is important.
cmd.Parameters.Add("@Name", OleDbType.VarChar).value = "SLaks"
cmd.Parameters.Add("@City", OleDbType.VarChar).value = "New-York"

Dim da as new OleDbDataAdapter(cmd)

' Here you can use the Data Adapter as you would normally do.

希望对您有所帮助。

关于vb.net - 传递参数以查询 Access 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7472563/

相关文章:

vb.net - 如何访问这个变量?

c# - 使用 newtonsoft.json 反序列化 List<AbstractClass>

mysql - VB.Net,按用户输入的范围过滤DataGridView

vb.net - 在 VB.NET 中使用 Firefox HTML 源查看器?

delphi - 在 Delphi 的 Unicode 版本中 Access 记录的真实缓冲区 - ADO

jquery - 仅当 '/' 是自动完成的最后一个字符时,如何调用 Web 方法?

c# - 将 byte[] 作为文件打开,而不是首先将其实际保存为文件

sql-server - 用于插入 SQL Server 的 VBA 空字符串

excel - MS Access Nz() 函数无法在 MS Excel 中识别

Java 和 Access 数据库