我尝试使用 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
最佳答案
您的问题是您从 C# 代码传递的日期是 18/02/2015。 Mysql 只排除 YYYY-MM-DD 格式的日期。如果您想将其存储为日期,您需要调整该数据,使其格式符合 Mysql 的格式(日期除外)。
我实际上编写了一个您也许可以使用的存储过程(或者至少了解需要做什么):Here is the link .
此外,如有疑问,请参阅 dev.mysql它也是一个很好的资源。
关于c# - C#中MySql命令 "load data infile"执行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28671049/