Home » excel » c# – OleDbDataAdapter datatype mismatch using Jet

c# – OleDbDataAdapter datatype mismatch using Jet

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m using an OleDbDataAdapter (Microsoft.ACE.OLEDB.12.0 to be precise) to retrieve data from an Excel workbook. For one table I’m using a typed dataset but for another table I can’t do that since the number of columns is unknown (the Excel template may generate extra columns).

The problem was that if someone enters too many numeric values in a column, “JET” seems to assume it’s a numeric column and the textual values are not loaded anymore. I know you can change the template and set the specific data type for that column but the template is already widely spread so I’d rather resolve it during import.

Now what I tried was first counting the number of used columns and preparing a new DataTable with a defined Columns collection and setting their DataType property to typeof(string). Sadly JET doesn’t seem to be looking at this and still chooses it’s own way. I’m guessing that even if I could use a strongly typed dataset here, it wouldn’t help either…

Does anyone know how to tell JET how to import the data so I don’t have to face the burden of delivering a new template version?

Please *PLEASE*: don’t come with an Excel automation solution…

How to&Answers:

If you have access to the registry set TypeGuessRows=0 and/or ImportMixedTypes=Text. See here for more info INITIALIZATION SETTINGS