【发布时间】:2011-12-12 20:43:33
【问题描述】:
如何使用 OLEDB 解析和导入每个单元格都用双引号括起来的 CSV 文件,因为有些行中包含逗号?我无法更改格式,因为它来自供应商。
我正在尝试以下操作,但由于 IO 错误而失败:
public DataTable ConvertToDataTable(string fileToImport, string fileDestination)
{
string fullImportPath = fileDestination + @"\" + fileToImport;
OleDbDataAdapter dAdapter = null;
DataTable dTable = null;
try
{
if (!File.Exists(fullImportPath))
return null;
string full = Path.GetFullPath(fullImportPath);
string file = Path.GetFileName(full);
string dir = Path.GetDirectoryName(full);
//create the "database" connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"text;HDR=No;FMT=Delimited\"";
//create the database query
string query = "SELECT * FROM " + file;
//create a DataTable to hold the query results
dTable = new DataTable();
//create an OleDbDataAdapter to execute the query
dAdapter = new OleDbDataAdapter(query, connString);
//fill the DataTable
dAdapter.Fill(dTable);
}
catch (Exception ex)
{
throw new Exception(CLASS_NAME + ".ConvertToDataTable: Caught Exception: " + ex);
}
finally
{
if (dAdapter != null)
dAdapter.Dispose();
}
return dTable;
}
当我使用普通的 CSV 时,它可以正常工作。我需要更改 connString 中的某些内容吗??
【问题讨论】:
-
关于错误的更多信息?
-
@Christopher rathermel 这是一个 IErrorInfo.GetDescription 失败异常