0

下面的代码给了我一个参数异常说,显然在 csv 中的 sourcecolumn 'e_partnerid' 不存在。我有一种感觉分隔符设置不正确或类似的东西,我尝试稍微更改连接字符串,但我仍然得到同样的错误。

static DataTable GetDataTableFromCsv(string path, string csvSelection, bool isFirstRowHeader)
    {
        string header = isFirstRowHeader ? "Yes" : "No";


        string pathOnly = Path.GetDirectoryName(path);
        string fileName = Path.GetFileName(path);
        //string sql = @"SELECT " + csvSelection + " FROM [" + fileName + "];";
        string sql = @"SELECT " + "*" + " FROM [" +  fileName  + "];";

        using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + ";FMT=Delimited(;)\""))
        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;
        }

    }

我用 ; 作为 csv 内部的分隔符,甚至将其设置在 connstring 中,但仍然是相同的例外。

4

1 回答 1

0

我需要制作一个描述列的 schema.ini:

private static void writeSchema(DataColumnCollection columns, string csvDir, string csvFileName)
    {
        FileStream fsOutput =
                 new FileStream(csvDir + "\\schema.ini",
                                     FileMode.Create, FileAccess.Write);
        StreamWriter srOutput = new StreamWriter(fsOutput);

        srOutput.WriteLine("[" + csvFileName + "]");
        int i = 1;
        foreach (DataColumn item in columns)
        {
            srOutput.WriteLine("Col" + i + "=\"" + item.ToString() + "\" Text");
            i++;
        }
        srOutput.Close();
        fsOutput.Close();
    }
于 2014-03-28T10:16:33.177 回答