none
How can I make OleDbDataReader read a field as a System.String instead of System.Double? RRS feed

  • Question

  • Hello,

    I'm using an OleDbDataReader to read an Excel file.  All is working well, except my OleDbDataReader thinks the first column's type is System.Double.  I don't know why.

     

    The reader successfully reads a cell when it can convert the cell's value to a number, but if the cell contains an alphabetic character (e.g. 123A), the reader contains null.  I'd like it to contain "123A".

     

    How can I configure the reader to to read the first column's type as System.String?

     

    Thanks,

    Mike

     

    Monday, June 30, 2008 3:24 AM

Answers

All replies

  • Have a look at this: ExcelConnectionString

    Monday, June 30, 2008 8:14 AM
  • Thanks, that fixed it!

    Mike

    Monday, June 30, 2008 5:09 PM
  • Glad to help you Smile
    Monday, June 30, 2008 5:11 PM
  • When you use Jet OLDB provider to connect to Excel file, provider tries to identify type of each column inside of spreadsheet based on scanning first rows inside of spreadsheet. It needs this because Excel spreadsheet is not really structured table and may contain different types of data in same column. After that, when provider reads values and specific cells contain values that do not match defined type, provider returns NULL. What you could do is to set IMEX=1 property inside of Extended Properties of your connection string. It will force provider to read all the values as strings. In this case you would need to parse string values yourself. If it does not fit your needs, you could try .NET Managed reader from my web site. It provides functionality to read individual cells and it also returns all the values inside of DataTable in their native format from Excel file

     

    Tuesday, July 1, 2008 2:21 AM
    Moderator