简体   繁体   中英

C# Mysql with Fatal error

The script works fine in the Workbench but when I use it in C# I get " Fatal error encountered during command execution. "

I can not see the error, but I believe it is by "@"

cmd.CommandText = "CREATE TABLE temp_update_table (matricula VARCHAR(50) NOT NULL, nome VARCHAR(150) NOT NULL, cargo VARCHAR(50) NOT NULL, nasc VARCHAR(50) NOT NULL, admissao VARCHAR(50) NOT NULL, cpf VARCHAR(50) NOT NULL, status VARCHAR(50) NOT NULL);";
cmd.ExecuteNonQuery();
cmd.CommandText = @"LOAD DATA LOCAL INFILE '//10.5.0.2/c$/dhcp.txt' INTO TABLE temp_update_table FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 LINES (@col1, @col2) set matricula = @col1, nome = @col2;"; 
cmd.ExecuteNonQuery();

When I do the script below, it works:

Cmd.CommandText = "LOAD DATA LOCAL INFILE '//10.5.0.2/c$/dhcp.txt' INTO TABLE temp_update_table FIELDS TERMINATED BY ';' LINES TERMINATED BY '\\n' IGNORE 1 LINES;";

But when I put the end it generates the error " Fatal error encountered during command execution. ":

... LINES (@col1, @col2) set matricula = @col2, name = @co1;";

I was building a .NET C# application with a MySQL database, and I too kept running into the error message:

"Fatal error encountered during command execution."

When calling the "LOAD DATA INFILE" MySQL command from my .NET application. It was very hard to figure out the solution, so I wanted to respond here with the solution that worked for me. Eventually, after several days of reading everything I could find about the problem (including this post), I was able to fix it...and the solution was not intuitive by any means, and it ended up being two different issues!

The first fix I found was that, when you're using variables in your INFILE, you need to add the following statement to your connection string (believe it or not):

"Allow User Variables=True;"

So, that means your complete connection string should look something like this:

"server=127.0.0.1;uid=user;pwd=password;database=mysql_db_name;Allow User Variables=True"

This surprised me, but seemed to work!! I found that solution here: MySQL fatal error encountered during command execution - looping through all csv files in folder for load data local infile

However, after I modified the connection string, although most input files started working for me, I was STILL getting this error message intermittently. I would get the error message "Fatal error encountered during command execution" for some input files and not others?? After reading more and playing around with things, it seemed to be a problem of the connection timing out (or something weird like that), even though the error message did NOT say anything about a connection timeout. I was able to stop the error message from popping up and correctly import data files into MySQL by increasing the "CommandTimeout" property on the database "MySqlCommand" object in C#, like this:

mysqlCommand.CommandTimeout = 600;

So, the full code looked something like this:

string strConnect = "server=127.0.0.1;uid=user;pwd=password;database=mysql_db_name;Allow User Variables=True";
Using (MySqlConnection mysqlConnection = new MySqlConnection(strConnect))
{
    Using (MySqlCommand mysqlCommand = new MySqlCommand())
    {
        mysqlCommand.CommandTimeout = 600;
        mysqlCommand.Connection = mysqlConnection;
        mysqlCommand.CommandType = CommandType.Text;

        mysqlCommand.CommandText = "LOAD DATA INFILE (...your specific command here)";

        try
        {
            mysqlConnection.Open();
            mysqlCommand.ExecuteNonQuery();
            mysqlConnection.Close();
        }
        catch(Exception ex)
        {
            // handle errors
        }
    }
}

And I found that particular solution after reading this post: Sourcing 'Fatal Error' resulting from MySQL ExecuteNonQuery?

Anyway, I think that was everything I needed to do to solve my problem. Hopefully this information is helpful for you too. Good luck and happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM