Search code examples
c#ms-accessoledb

How to use named parameters in a TableAdapter query?


I am configuring a dataset through the query wizard. I want to generate the parametrized query. My query looks like this:

SELECT 
    Field1, Field2, Field3 
FROM
    SomeTable
WHERE
    Field1 = @field1

The data is being fetched from an Access 2007 database, where this query executes successfully. From code however, I am getting the error:

Error in WHERE clause near '@'. Unable to parse query text.

How can I solve this?


Solution

  • Access doesn't support named parameters and uses ? instead of @ (like SQL-Server).

    So this should work:

    ...
    WHERE
        Field1 = ?
    

    See also How to: Create Parameterized TableAdapter Queries:

    When constructing a parameterized query, use the parameter notation specific to the database you are coding against.

    For example, Access and OleDb data sources use the question mark '?' to denote parameters, so the WHERE clause would look like this: WHERE City = ?.