for how to write parameterized queries.
Search with oledb
Hello
I use Access Database Engine and OleDbReader to search within my mdb database:
New OleDbCommand("SELECT * From Blah WHERE TheID = ?" + strCondition, conn)
strCondition can vary based on the user input:
strCondition = " AND ([strFieldName] = '" + termTextBox.Text.Trim.Replace("'", "''") + "')"
or
strCondition = " AND ([strFieldName] Like '%" + termTextBox.Text.Trim.Replace("'", "''") + "%')"
or...
Need to use parameterized queries to pass termTextBox.Text to command and have no idea how to proceed?
Any help or idea please?
Thanks in advance :)
3 answers
Sort by: Most helpful
-
-
Jiachen Li-MSFT 30,686 Reputation points Microsoft Vendor
2024-06-18T06:31:23.68+00:00 Hi @StewartBW ,
When using
OleDbCommand
with parameters in your SQL query, you can specify parameters using placeholders (?
) in your SQL statement. However, OleDb does not support named parameters; instead, it uses positional parameters. This means the parameters must be added in the exact order they appear in the SQL query.Check OleDbParameterCollection.AddWithValue(String, Object) Method.
Using cmd1 As New OleDbCommand("SELECT * From Blah WHERE TheID = ? AND ([strFieldName] Like '%" + termTextBox.Text.Trim.Replace("'", "''") + "%')" + strCondition, conn) cmd1.Parameters.AddWithValue("@TheID", theIDValue) ' Replace theIDValue with the actual value for TheID ... End Using
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
-
StewartBW 1,145 Reputation points
2024-06-19T00:53:31.6533333+00:00 Hello and thanks so much, I meant how to pass the termTextBox.Text via parameter?