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;
}
}
xls
,xlsx
) files, I would prefer to leverage NPOI, you could refer to this sample and issue. For reading the csv files, you could use LumenWorks.Framework.IO, CsvHelper, etc. – Bruce Chen