Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown issue when inserting #1145

Closed
andrewdci03 opened this issue Feb 24, 2022 · 2 comments
Closed

Unknown issue when inserting #1145

andrewdci03 opened this issue Feb 24, 2022 · 2 comments

Comments

@andrewdci03
Copy link

andrewdci03 commented Feb 24, 2022

Software versions
MySqlConnector version:
Server type (MySQL, MariaDB, Aurora, etc.) and version:
.NET version:
(Optional) ORM NuGet packages and versions:

Describe the bug
A clear and concise description of what the bug is.

Exception

143 rows have been copied but 0 has been inserted

Code sample

` var connection = new MySqlConnection("[redacted]");

        //creating object of MySqlBulkCopy
        MySqlBulkCopy objbulk = new MySqlBulkCopy(connection);

        //Mapping Table column
        MySqlBulkCopyColumnMapping columnMapping;
        foreach (var column in csvdt.Columns)
        {
            columnMapping = new MySqlBulkCopyColumnMapping(csvdt.Columns[column.ToString()].Ordinal, column.ToString());
            objbulk.ColumnMappings.Add(columnMapping);
        }

        //inserting Datatable Records to DataBase
        connection.Open();
        MySqlConnector.MySqlBulkCopyResult result = objbulk.WriteToServer(csvdt);
        //test.Load();
        connection.Close();`

Expected behavior
I expected for it insert the rows

Additional context
It's a csv file that's being inserted into a MariaDB. This function is being called in a Foreach loop and inserts different csv files into different tables based on the file name. It inserts the first csv file and then fails on the remaining. I have forced it to skip the first file and start on the second and it still fails.

@bgrainger
Copy link
Member

Hmm, it may be the case that the errors aren't exposed if the Write method throws an exception; cf. #1012 (comment).

Please try this workaround:

var connection = new MySqlConnection("[redacted]");
connection.InfoMessage += (s, e) =>
{
	// use logging infrastructure of your choice
	foreach (var error in e.Errors)
		Console.WriteLine(error.Message);
};

// ...

connection.Open();
MySqlConnector.MySqlBulkCopyResult result = objbulk.WriteToServer(csvdt);

Does the InfoMessage event handler provide enough information to diagnose the data issue causing no rows to be inserted?

@andrewdci03
Copy link
Author

@bgrainger that looks like it did it!

Apparently it's violating a foreign key constraint. I'll share the output snippet below:
"Cannot add or update a child row: a foreign key constraint fails..."

So I should be able to make some adjustments from that. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants