使用OleDbDataAdapter读取CSV文件时出现未指定的错误

时间:2015-04-15 20:31:24

标签: asp.net console-application impersonation jet oledbdataadapter

更新1:我认为schema.ini不正确。请参考以下问题。 文件(dsTextFile)只有一行数据,但记录计数为零。所以这意味着它根本就不读书。这是完全删除FMT或与Delimi删除。如果修复了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;
        }
    }

请找到我收到错误的上述来源'未指定"对于下线。

未指定错误来自以下行 int rows = textDataAdapter.Fill(dsTextFile)

可能是什么问题?我已经检查了c:\ windows \ temp的用户权限,但没有成功。

这是一个控制台应用程序,我甚至尝试在app.config中添加以下代码但尚未成功。

<system.web>
<identity imperonate = "false"/> </system.web>

这是一个遗留应用程序,但需要在Windows Server 2012安装程序上运行。

1 个答案:

答案 0 :(得分:0)

您在连接字符串上使用FMT = Fixed,如果您没有使用schema.ini,请将其更改为FMT = Delimited,它将起作用。

即:

string textConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + textFileFolder + ";" +
"Extended Properties=\"text;HDR=No;FMT=Delimited\";";