OleDbCommand parameters order and priority
Asked Answered
W

3

17

I have been debugging this query for the last 40 minutes, and the problem apparently is the order of the parameters after all.

SELECT * FROM tblSomeThing WHERE id = @id AND debut = @dtDebut AND fin = @dtFin

Then I add the parameters this way, notice that the two last parameters are switched, I get no results.

cmd.Parameters.Add("@id", OleDbType.Integer).Value = idSociete;
cmd.Parameters.Add("@dtFin", OleDbType.Date).Value = dateTraitementFin;
cmd.Parameters.Add("@dtDebut", OleDbType.Date).Value = dateTraitementDebut;

When I declare the parameters the way they appear in the queury everything works perfectly.

I thought named parameters were at first place to address this problem! what am I missing here?

Thank you

Workbook answered 25/9, 2009 at 11:26 Comment(0)
A
31

According to http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx OleDbCommand does not support named parameter

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

So order of parameter is important.

Archiphoneme answered 25/9, 2009 at 11:37 Comment(1)
bizare, so the name of the parameters have nothing to do in the query at the end!!Workbook
A
3

If I recall correctly, if the OleDbCommand in ADO.NET functions similarly to the older ADO library/libraries (used in VB6, VBA, etc.) then the parameter collection does not define parameters by name, only by position within the collection. This seems to be the behaviour you are experiencing.

Applicative answered 25/9, 2009 at 11:36 Comment(0)
U
0

Not positive, but it doesn't look like your parameters are added in the same sequence, nor same named values as their "@" counterparts of the query...

@id,
@dtDebut 
then
@dateTraitementFin
Ultraconservative answered 25/9, 2009 at 11:32 Comment(1)
yeah that's my question, why I have to add them with the same order as the @ in the query?Workbook

© 2022 - 2024 — McMap. All rights reserved.