When Odp.net is used, the specified conversion is invalid

Source: Internet
Author: User

Doing database backup and restore.

I used odp.net to return the data table directly to the dataset using the stored procedure +oracledataadapter, but prompted "invalid conversion specified". I wonder why the report application and query, do not report this error, and export the physical table, the report this error.

Check the information to find out why:

The OracleDataAdapter class attempts to maps Oracle native data types to. NET Data types

Number, DATE, TIMESTAMP, TIMESTAMP with the LOCAL time zone, TIMESTAMP with time zone, INTERVAL day to SECOND

Some of the above types may cause exceptions due to database accuracy and. NET precision. For example, the database number is 38 bits, but Decemal is only 28 bits.

Oracle decided, the best-of-the-these types of data in a Dataset object without losing any detail would be eithe R as a byte array or a string

Oracel recommended user manually define this mapping!

_myadapter.safemapping.add ("Launchdate", typeof (String)); _myadapter.safemapping.add ("Expirydate", typeof (Byte[]));

So many tables, how do I know which column has the wrong accuracy? All my data is of type number. So I wrote a piece of code,

Use OracleDataReader to loop through the columns to find the problem column!

Oracledatareader reader = cmd. ExecuteReader (); Int x = 0;do{    trace.writeline ("Table"  +  (++x));     while  (reader. Read ())     {        string result =  reader. GetString (3)  +  "-"  + reader. GetString (4)  +  "+";        for  (int j =  6; j < reader. fieldcount - 1; j++)         {             try             {                 if  (!reader. IsDBNull (j)  && reader. GetFieldType (j) ==typeof ( decimal))                        result +=  (reader. Getdecimal (j). ToString ()  +  ",");                 else                     result +=  ("null,");             }            catch   (EXCEPTION&NBSP;EX)             {                 trace.writeline ("wrong  filed  "+j+"     "+result";                 conn. Close ();                throw;            }         }        trace.writeline (Result);     }} while  (reader. NextResult ());

Finally found the problem column!

In fact, a few points of oracledatareader knowledge.

Reader. Read () reader. IsDBNull (j) Reader. GetFieldType (j) Reader. NextResult ()

NextResult is the loop between multiple result sets. Because my procedure returns 20 cursor sets at a time.

You see, with OracleDataReader is so complicated, but I have found the problem column, from the library to modify the data into the accuracy within. You can do this:

OracleDataAdapter ODA = new OracleDataAdapter (cmd);

Oda. Fill (DS);

In two words, all the tables are dumped into the dataset at once. Don't be too cool, okay?







When Odp.net is used, the specified conversion is invalid

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.