Development and Programming
C# and .NET
Using Named Parameters with Oracle ODP.NET
The Oracle data provider always binds parameters by position unless told otherwise. So, even though we have named parameters in our query, and we have added named parameters to the OracleCommand object, the data provider still binds the parameters by position; and as we have added the parameters in the 'wrong' order (albeit deliberately in this example) the query doesn't throw an exception, it merely returns an empty result set as the query received by the database is the equivalent of:
SELECT SOME_COLUMN, ANOTHER_COLUMN, THIRD_COLUMN FROM SOME_TABLE WHERE ANOTHER_COLUMN = 'Ping' AND THIRD_COLUMN = 'Foo'
This isn't made entirely clear on the Oracle documentation, but there is an additional property, BindByName, on the OracleCommand object, which must be set to true in order to bind parameters by name:
OracleCommand command = new OracleCommand(query, connection)
CommandType = CommandType.Text,
BindByName = true
© 2018 Digcode.com. All rights reserved.