web analytics

Using Named Parameters with Oracle ODP.NET

Options

codeling 1595 - 6639
@2018-07-10 15:25:54

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

};

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com