The latest project uses oracle db. In. net, you can use Oracle client to execute SQL commands on Oracle DB. Write the following program code to obtain the user data in the user data table.
using (OracleConnection connection = new OracleConnection(connectionString)){ connection.Open(); using (OracleCommand command = new OracleCommand()) { command.Connection = connection; command.Parameters.Add(":lastName", "Chou"); command.Parameters.Add(":firstName", "Clark"); command.CommandText = @"SELECT * FROM USER_DATA WHERE FIRST_NAME=:firstName AND LAST_NAME=:lastName"; using (OracleDataReader reader = command.ExecuteReader()) { while (reader.Read()) { // XXXXXXX } connection.Close(); } }}
Quickly complete and press to execute. But produced a confusing error exception "ORA-01722: Invalid numbers 」.
It took only half a day to find that adding Parameters Using oraclecommand. parameters is sequential. The order of parameters in oraclecommand. commandtext must be consistent with that in oraclecommand. parameters. That is, the following program code can be executed normally:
using (OracleConnection connection = new OracleConnection(connectionString)){ connection.Open(); using (OracleCommand command = new OracleCommand()) { command.Connection = connection; command.Parameters.Add(":firstName", "Clark"); command.Parameters.Add(":lastName", "Chou"); command.CommandText = @"SELECT * FROM USER_DATA WHERE FIRST_NAME=:firstName AND LAST_NAME=:lastName"; using (OracleDataReader reader = command.ExecuteReader()) { while (reader.Read()) { // XXXXXXX } connection.Close(); } }}
Discuss the Problem and Solution with your friends on the Internet. Through the demo, we can know that we can solve this problem by setting oraclecommand. bindbyname to true. The sample program is as follows:
using (OracleConnection connection = new OracleConnection(connectionString)){ connection.Open(); using (OracleCommand command = new OracleCommand()) { command.BindByName = true; command.Connection = connection; command.Parameters.Add(":lastName", "Chou"); command.Parameters.Add(":firstName", "Clark"); command.CommandText = @"SELECT * FROM USER_DATA WHERE FIRST_NAME=:firstName AND LAST_NAME=:lastName"; using (OracleDataReader reader = command.ExecuteReader()) { while (reader.Read()) { // XXXXXXX } connection.Close(); } }}
While there is still a quick record of impressions, I want to keep a record for myself and help developers who need it. Thanks again for the demo. : D