Passing parameter to query for Access database

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.

I hope this helps.

Leave a Comment