传递参数以查询 Access 数据库 [英] Passing parameter to query for Access database

查看:23
本文介绍了传递参数以查询 Access 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using following code and trying to get data by given parameters. I donot know how to pass the parameter value to my query.

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 ProgramsDB ExperimentsAddressBook.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

I would first create an OleDbCommand object and use this object to create a 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.

我希望这会有所帮助.

这篇关于传递参数以查询 Access 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆