Passing parameter to query for Access database
19,259
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.
Comments
-
Furqan Sehgal almost 2 years
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 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()