MySqlBulkLoader with Column Mapping?

bulkinsert c# mysql sqlbulkcopy

Question

I utilizeSqlBulkCopy to put data into a SQL Server database in bulk. The closest thing to MySql support for my software is now available from me.SqlBulkCopy is MySqlBulkLoader . But inMySqlBulkLoader I first need to convert myDataTable to a file sinceMySqlBulkLoader only works with files, notDataTable . Before inserting, I must then turn off foreign key checks. I completed them both, but now I have another issue.

An identification column in my destination table (PK and auto-increment) andMySqlBulkLoader only the first record is entered with the incorrect column mappings since the source file's first column is mapped to this column. If it helps, here is how I use the function:

using (var conn = new MySqlConnection(connectionString))
{
    var bl = new MySqlBulkLoader(conn);
    bl.TableName = tableName;
    bl.Timeout = 600;
    bl.FieldTerminator = ",";
    bl.LineTerminator = "\r\n";
    bl.FileName = tempFilePath;
    bl.NumberOfLinesToSkip = 1;
    numberOfInsertedRows = bl.Load();
}

The first few lines of my file are as follows:

CampaignRunId,RecipientId,IsControlGroup
27,"testrecipient_0",False
27,"testrecipient_1",False
27,"testrecipient_2",False
27,"testrecipient_3",False
27,"testrecipient_4",False
27,"testrecipient_5",False
27,"testrecipient_6",False
27,"testrecipient_7",False
27,"testrecipient_8",False
27,"testrecipient_9",False
27,"testrecipient_10",False
27,"testrecipient_11",False
27,"testrecipient_12",False
27,"testrecipient_13",False

Is it possible to provide column mapping forMySqlBulkLoader ? It seems to have aColumns however it only has read-only access.

There is a library out there with the name MySqlBulkCopy, but I had other issues with it and it is not from a reliable source.

1
0
2/24/2015 9:00:40 AM

Popular Answer

Expanding on David Hartley's response, you may just empty the list of columns and add stuff to it from scratch if you don't know the column names beforehand.

The format will like this:

bl.Columns.Clear(); foreach (string col in yourColumns) { bl.Columns.Add(col); }

1
11/3/2016 3:01:05 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