I have a need to download a CSV file from a network location and import it into a MySQL table. The absolute easiest way is to copy it to the MySQL location and use LOAD DATA. I did it manually using this command:
LOAD DATA INFILE 'Transactions.CSV'
INTO TABLE tmpImport
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(AccountDesignator,@PostedDate,SerialNum,Description,Amount,CrDr)
SET PostedDate = STR_TO_DATE(@PostedDate, '%m/%d/%Y');
I had to add the @PostedDate because, without it, MySQL complained that the date was not in the correct format.
So since this is going to be a regular thing, I tried to put it into a stored procedure that could be called by my process, but using LOAD DATA in a stored procedure is not allowed.
So I figured I'd just send the raw command from my process using the MySQL Net/Connector:
using (MySqlCommand cmd = new MySqlCommand(sql, new MySqlConnection("server=theServer; uid=theUid; pwd=thePwd; database=theDatabase; SslMode=none;")))
{
cmd.Connection.Open();
rows = cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
But when I try to run that, I get this error:
MySqlException: Parameter '@PostedDate' must be defined.
Is there any way around this? Or am I going to have to resort to reading in the CSV file and inserting the records individually?