Using Named Parameters with Oracle ODP.NET Options

codeling Posts: 1057 Points: 4443
Posted: Tuesday, July 10, 2018 3:25:54 PM

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'

 

The solution

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

};

 
Users browsing this topic
Guest