我正在尝试导入一个文本文件到MySql数据库使用C#代码,但得到错误。我的表结构是:

我正在执行的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发布于 2015-02-25 09:10:52
您的问题是您从C#代码中传递的日期是18/02/2015。Mysql仅排除YYYY-MM-DD格式的日期。如果您希望将数据存储为日期,则需要调整该数据,使其格式与Mysql的格式相同。
实际上,我编写了一个存储过程,您或许可以使用它(或者至少知道需要做什么):Here is the link。
当你有疑问的时候,可以参考dev.mysql,它也是一个很棒的资源。
https://stackoverflow.com/questions/28671049
复制相似问题