c# - C#中MySql命令 "load data infile"执行错误

标签 c# mysql mysql-error-1064 load-data-infile

我尝试使用 C# 代码将文本文件导入 MySql 数据库,但出现错误。 我的表结构是: Mysql database table

我正在执行的 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/

相关文章:

c# - 带有 class 和 new() 关键字的通用方法

c# - 如何使用 VSIX 扩展实现控制台应用程序

c# - 使用 Windows 通用应用程序将图像上传到 mysql 数据库

mysql - h2 数据库按位操作(&)语法无效 MySql

Mysql二叉树选择查询优化

c# - 真正的 MVVM 和第三方控件

mysql - 当我输入 non 时,它需要显示 1 和 12 年

MySQL, 'WHERE NOT IN' 慢

mysql - 使用 where 子句中的 select 查询中的字段之一更新查询

php - 由不应该存在的奇怪数组值引起的 MySQL 语法错误?