[. Net] Oracle client exception-ORA-01722

Source: Internet
Author: User
Tags oracleconnection

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.