0

The MySQL command works fine in MySQL itself but when I do it over my c# program, it says "Error 0 has occurred: Fatal error encountered during command execution.".

Is there anything I did wrongly here with my c# code? My code loops through all the csv files in the folder, then import csv data into MySQL database. Any advice would be much appreciated!

private void btn_importCSV_Click(object sender, EventArgs e)
{
    var CSVfolder = Directory.EnumerateFiles(sourceDirCSV);
    foreach (string CSVfile in CSVfolder)
    {
        try
        {
            using (var connection = new MySqlConnection(strProvider))
            using (var command = connection.CreateCommand())
            {
                connection.Open();
                command.CommandTimeout = 5000000;
                command.CommandText = @"load data local infile 'C:\\Full\\Path\\To\\" + CSVfile + "' into table prices fields terminated by ',' lines terminated by '\n' (@date, Prices_Time, Prices_Open, Prices_High, Prices_Low, Prices_Close, Prices_Volume, Tickers_Ticker_ID) set Prices_Date = str_to_date(@date, '%e/%c/%Y');";
                command.ExecuteNonQuery();                        
                connection.Close();
            }
        }
        catch (MySqlException ex)
        {
            MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message,
                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }  
    }

    DialogResult dialogResult = MessageBox.Show("Done importing into MySQL database! Do you want to view the data via DataGrid table?", "View Table in DataGrid", MessageBoxButtons.YesNo);
    if (dialogResult == DialogResult.Yes)
    {
        FillGrid_FetchSharePrices();
    }
    else if (dialogResult == DialogResult.No)
    {
        //Do nothing
    }
}
0

1 Answer 1

1

Check that your connection string includes the following:

"Allow User Variables=True;"

This solved the problem for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.