Question

the following code gives me an argumentexception saying, that the sourcecolumn 'e_partnerid', which is clearly in the csv, doesn't exist. I have a feeling that the Delimiter isn't set right or something like that and I tried changing the connectionstring a little, but I still get the same error.

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;
        }

    }

I use ; as Delimiter inside of the csv and even set it in the connstring, but still the same exception.

Was it helpful?

Solution

I needed to make a schema.ini that describes the columns:

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();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top