MS Access, Named parameters and Column Names

后端 未结 2 1533
我寻月下人不归
我寻月下人不归 2020-12-12 02:49

I have the following query which I am executing on an Access database. The query, when run in Access returns accurate results. However when run from the code I get back all

2条回答
  •  伪装坚强ぢ
    2020-12-12 03:37

    The way you have done it, OleDb is using positional parameter insertion, so your first parameter in SQL, '@EndDate' is being substituted with the first parameter passed, '@StartDate'. The names of the parameters are completely ignored when using positional insertion.

    However, it's a little-known fact that OleDb actually DOES accept named parameters. You've just got to declare the parameters in SQL as well.

    See: low-bandwidth.blogspot.com.au/2013/12/positional-msaccess-oledb-parameters.html

    If you DON'T declare the parameters in SQL, OleDb uses purely positional parameter insertion, and it doesn't matter if the names of the parameters match the SQL, or if parameters are used twice in the SQL - it will just go through and blindly replace any found parameters in the SQL in order from start to end, with those passed.

    However if you DO declare the parameters correctly, you get the benefit of named parameters and parameters allowed to be repeated multiple times within the SQL statement.

提交回复
热议问题