Reputation: 4997
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()
Upvotes: 3
Views: 20144
Reputation: 11084
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.
Upvotes: 3
Reputation: 25272
Why not directly build the final Sql ?
sql = "SELECT * FROM tblContacts where Name='" & myName & "' and City='" & myCity & "'"
Would there be a difference in performance or security or...?
Upvotes: -1
Reputation: 888203
Call
da.SelectCommand.Parameters.AddWithValue("paramName", someValue);
Upvotes: -1