gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:33:56 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com