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

后端 未结 4 667
情书的邮戳
情书的邮戳 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:19

    As you have discovered, OLEDB uses Jet which is limited in the manner in which it can be tweaked. If you are set on using an OleDbConnection to read from an Excel file, then you need to set the HKLM\...\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows value to zero so that the system will scan the entire resultset.

    That said, if you are open to using an alternative engine to read from an Excel file, you might consider trying the ExcelDataReader. It reads all columns as strings but will let you use dataReader.Getxxx methods to get typed values. Here's a sample that fills a DataSet:

    DataSet result;
    const string path = @"....\Test.xlsx";
    using ( var fileStream = new FileStream( path, FileMode.Open, FileAccess.Read ) )
    {
        using ( var excelReader = ExcelReaderFactory.CreateOpenXmlReader( fileStream ) )
        {
            excelReader.IsFirstRowAsColumnNames = true;
            result = excelReader.AsDataSet();
        }
    }
    

提交回复
热议问题