0

On Azure App service, my application use Microsoft.Jet.OLEDB.4.0 to read my file with japanese file name. It worked well until 2017/09/21, but it throw exception from 2017/09/22. It only read file without japanese file name. If to read one with japanese file name, it throw exceptions are as follows

System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)

I asked Microsoft support site, but no useful answer. Has my codes some wrong ?

my code:

   public static DataTable readCSV(string filePath)
    {
        DataTable dt = new DataTable();
        if (File.Exists(filePath) == false)
        {
            return dt;
        }

        string header = "No";
        string pathOnly = Path.GetDirectoryName(filePath);
        string fileName = Path.GetFileName(filePath);

        FileInfo fInfo = new FileInfo(filePath);
        FileInfo dinfo = new FileInfo(pathOnly);
        if (dinfo.IsReadOnly)
        {
            dinfo.IsReadOnly = false;
        }

        if (fInfo.IsReadOnly)
        {
            fInfo.IsReadOnly = false;
        }
        var encoding = Encoding.GetEncoding(932);
        using (StreamReader reader = new StreamReader(filePath, Encoding.GetEncoding(932), true))
        {
            reader.Peek(); // you need this!
            encoding = reader.CurrentEncoding;
        }
        string sql = @"SELECT * FROM [" + fileName + "]";

        using (OleDbConnection connection = new OleDbConnection(
                  @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
                  ";Extended Properties=\"Text;IMEX=1;CharacterSet=" + encoding.CodePage + ";HDR=" + header + "\""))
        using (OleDbCommand command = new OleDbCommand(sql, connection))
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
        {
            DataTable dataTable = new DataTable();
            dataTable.Locale = CultureInfo.CurrentCulture;
            adapter.Fill(dataTable);

            return dataTable;
        }
    }
jmdc
  • 3
  • 1
  • For reading excel (`xls`, `xlsx`) files, I would prefer to leverage [NPOI](http://npoi.codeplex.com/), you could refer to this [sample](https://npoi.codeplex.com/SourceControl/latest#NPOI.Examples/ImportXlsToDataTable/Form1.cs) and [issue](https://stackoverflow.com/questions/5855813/npoi-how-to-read-file-using-npoi). For reading the csv files, you could use [LumenWorks.Framework.IO](https://www.nuget.org/packages/LumenWorks.Framework.IO/), [CsvHelper](https://github.com/JoshClose/CsvHelper), etc. – Bruce Chen Oct 17 '17 at 09:27
  • @ Bruce Chen, thanks. Are you test NPOI can read japanese file with japanese filename ON Azure App service afer 2017/09/22? – jmdc Oct 18 '17 at 08:33
  • I did not test it for reading Japanese file yet, I would check it later. – Bruce Chen Oct 18 '17 at 09:40

1 Answers1

1

This might be caused by Windows security update KB4041681. At https://social.technet.microsoft.com/Forums/en-US/55b1d633-b715-491e-917e-b7cb01ae0523 several people have reported that uninstalling that update has solved their problems. Another solution is to use the Microsoft.ACE.OLEDB.12.0 driver instead of of Microsoft.Jet.OLEDB.4.0.