使用 OleDbDataAdapter 读取 CSV 文件时出现未指定的错误
Posted
技术标签:
【中文标题】使用 OleDbDataAdapter 读取 CSV 文件时出现未指定的错误【英文标题】:Getting unspecified error while Reading CSV file using OleDbDataAdapter 【发布时间】:2015-04-15 20:31:24 【问题描述】:更新 1: 我认为 schema.ini 不正确。请参考以下问题。 文件 (dsTextFile) 只有一行数据,但记录数为零。所以这意味着它根本没有阅读。这是完全删除 FMT 或使用 Delim。如果 FMT 已修复,我仍然会收到错误消息。那么,如何创建 SCHEMA.ini 或确保 schema.ini 正确?
private bool LoadTextFile(string textFilePath, out string errorInfo)
errorInfo = String.Empty;
try
string textFileFolder = (new System.IO.FileInfo(textFilePath)).DirectoryName;
string textConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + textFileFolder + ";" +
"Extended Properties=\"text;HDR=No;FMT=Fixed\";";
OleDbConnection textConnection = new OleDbConnection(textConnectionString);
textConnection.Open();
textFilePath = (new System.IO.FileInfo(textFilePath)).Name;
string selectCommand = "select * from " + textFilePath;
OleDbCommand textOpenCommand = new OleDbCommand(selectCommand);
textOpenCommand.Connection = textConnection;
OleDbDataAdapter textDataAdapter = new OleDbDataAdapter(textOpenCommand);
Console.WriteLine("Trying to set textDataAdapter");
int rows = textDataAdapter.Fill(dsTextFile); //This is where error is coming.
Console.WriteLine("detail rows being filled");
textConnection.Close();
textConnection.Dispose();
return true;
catch (Exception ex_load_text_file)
Console.WriteLine("error in loadTextFile is " + ex_load_text_file.Message.ToString());
return false;
请在下面的行中找到错误“未指定”的上述来源。
UnSpecified 错误出现在下面一行
int rows = textDataAdapter.Fill(dsTextFile)
可能是什么问题?我检查了 c:\windows\temp 上的用户权限,但没有成功。
这是一个控制台应用程序,我什至尝试在 app.config 中添加以下代码,但还没有成功。
<system.web>
<identity imperonate = "false"/> </system.web>
这是一个旧版应用程序,但需要在 windows server 2012 安装程序上运行。
【问题讨论】:
【参考方案1】:您在连接字符串上使用了 FMT=Fixed,如果您没有使用 schema.ini,请将其更改为 FMT=Delimited 即可。
即:
string textConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + textFileFolder + ";" +
"Extended Properties=\"text;HDR=No;FMT=Delimited\";";
【讨论】:
感谢您的意见。但我正在阅读的输入文件是固定长度的,没有分隔。这里重要吗? 我想你使用的是 schema.ini 文件呢? 是的,我正在使用 schema.ini 文件。 任何输入?如果我将 FMT 更改为分隔符,我不会收到原始帖子中提到的错误,但是在进一步解析数据时出现错误。 我认为 schema.ini 现在不正确。文件 (dsTextFile) 只有一行数据,但记录数为零。所以这意味着它根本没有阅读。这是完全删除 FMT 或使用 Delim。如果 FMT 已修复,我仍然会收到错误消息。那么,如何创建 schema.ini 或确保 schema.ini 正确?以上是关于使用 OleDbDataAdapter 读取 CSV 文件时出现未指定的错误的主要内容,如果未能解决你的问题,请参考以下文章