Mental Jetsam

By Peter Finch

Reading number fields form Oracle in C#

Posted by pcfinch on June 19, 2007

If you are using C# and are trying to read a field from an Oracle table defined as NUMBER then when you try to access the field from a C# OleDbDataReader using the GetInt32() member, you may get the a “Specified cast is not valid” exception. I found that reading a number from Oracle tables using the GetDecimal() works without an exception.

OleDbCommand cmd = db.createCommand(
  "Select TOPID_ID from TOPICS");
using(OleDbDataReader dr = cmd.ExecuteReader()) {
  while (dr.Read())
  {
    int nTopicId = Decimal.ToInt32(dr.GetDecimal(0));
    Console.WriteLine(nTopicId) ;
  }
}
Advertisements

One Response to “Reading number fields form Oracle in C#”

  1. qusai said

    I try it and it be correct

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: