How to force ADO.Net to use only the System.String DataType in the readers TableSchema

后端 未结 4 643
情书的邮戳
情书的邮戳 2021-01-17 15:08

I am using an OleDbConnection to query an Excel 2007 Spreadsheet. I want force the OleDbDataReader to use only string as the column datatype.

The system is looking a

4条回答
  •  野的像风
    2021-01-17 15:16

    I have faced the same issue and determined that this is something that many people commonly experience. Here are a number of solutions that have been suggested, many of which I have attempted to implement:


    1. Add the following to your connection string(Source):

    TypeGuessRows=0;ImportMixedTypes=Text

    1. Add the following to your connection string(Source, More Discussion, Even More):

    IMEX=1;HDR=NO;

    1. Edit the following registry settings, disable "TypeGuessRows", and "ImportMixedTypes" set to "Text"(Source, Not Recommended, More Documentation):

    Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/TypeGuessRows Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/ImportMixedTypes

    1. Consider using an alternative library for reading the excel file:

      • EPPlus
      • ExcelDataReader (also suggested be @Thomas)
      • OpenXml
    2. Format all data in the source file as Text(at least the first 8 rows), though I understand that's typically impractical(Source, though this is relation to SSIS, but it's the same concepts)

    3. Use a Schema.ini file to define the data type before importing the file, I found this in relation to using "Jet.OleDb" directly, maybe requiring you to modifying your connection string. This may only be applicable to CSV's I have not tried this approach.(Source, Related Post)


    None of these have worked for me(though I believe they have worked for others). I am of the opinion expressed by @Asher that there is really no good solution to this problem. In my software I simply display an error message to the user(if any required column contain empty values) instructing them to format all columns as "Text".

    Honestly, I think this book is more applicable to situation. The issue, already stated multiple times is:

    • "The data type at the destination is varchar but the assumed data type of "double" nullifies any data that doesn't fit."(Source)

    • "But the problem is actually with the OLEDBDataReader. The problem is that if it sees mostly numbers in a column, it assumes everything is a number - if a row item being read is not a number, it simply sets it to null! Ouch!"(Source)

    • "The problem seems to be with the JET engine itself and not ADO. Once JET decides on the type, it sticks to it."(@Asher)

    While I haven't found any of this documented in an official capacity I think that it's very clear that this is an intentional design decision and simply how the Jet Database Library works. I hesitate to call this library entirely useless because I think for many people some of these solutions do work, but so far for my project, I have come to the conclusion that this library cannot read multiple data types in a single column and is ill suited for general data retrieval.

提交回复
热议问题