我正在尝试使用C#代码将文本文件导入MySql数据库,但会出错。 我的表结构是:
我执行的C#代码是:
fileQuery =
"load data infile '{0}' into table dgl.deliveries fields terminated by '\t' lines terminated by \r\n' (@ImagePath, Delivery_Note, Shipment_Number, @Delivery_Date, Deliver_To_Code, Deliver_To_Name, Sold_To_Code, Sold_To_Name, Material_Number, Doctype) set Delivery_Date = tr_to_date(@Delivery_Date, '%d/%m/%Y'), ImagePath = Concat('USERFILES/', @ImagePath)";
string q = string.Format(fileQuery,fileName);
MySqlConnection conn = new MySqlConnection(dglConnection.ConnectionString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = q;
conn.Open();
command.ExecuteNonQuery();
,错误是:
An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.DLL but was not handled in user code
Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%d/%m/%Y'), ImagePath = Concat('USERFILES/', @ImagePath)' at line 2
以下是源输入文件中的一行:
123.pdf 802661341 1061611 18/02/2015 00:00:00 22280 ABC LIMITED 22280 XYZ LIMITED 30679795 30744488 DELIVERY NOTE 1
答案 0 :(得分:0)
您的问题是您从C#代码传递的日期是18/02/2015。 Mysql仅以YYYY-MM-DD格式排除日期。你需要调整这些数据,使其格式化为Mysql除日期外的方式,如果你想将它存储为日期。
我实际上编写了一个你可以使用的存储过程(或至少知道需要做什么):Here is the link。
另外,如果有疑问,请参考dev.mysql它也是一个很好的资源。