SqlBulkCopy ColumnMapping Argumentexception

columnmappings csv sqlbulkcopy

Question

The source column "e partnerid," which is obviously present in the csv, does not exist, according to the argumentexception that the following code raises. I tried slightly altering the connectionstring since I have a hunch that the Delimiter isn't set correctly or anything along those lines, but I continue to get the same issue.

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 set the delimiter ; in the connstring and use it as a delimiter inside the csv, but the error persists.

1
0
3/28/2014 2:03:28 PM

Accepted Answer

I had to develop a schema. It details the columns in 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();
    }
0
3/28/2014 10:16:33 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow