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
    }
}
merv
  • 331
  • 5
  • 25

1 Answers1

1

Check that your connection string includes the following:

"Allow User Variables=True;"

This solved the problem for me.

aleshu
  • 31
  • 4