Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

c# - MySQL Load Data Infile

I want to import CSV file into MySQL database but I am getting an error.

This is my method for LOAD DATA INFILE:

public int Import(string path)
{
   try
   {
      string cmd = "LOAD DATA INFILE " + path + " INTO TABLE zen_hardware.products FIELDS TERMINATED BY ',' LINES TERMINATED BY '
'";

      int a = MySqlHelper.ExecuteNonQuery(conn.Connect(),cmd);

      return a;
   }
   catch
   {
      return -1;
   }
}

When I run the code my string cmd gets this:

"LOAD DATA INFILE c:\users\trabajo\documents\visual studio 2013\Projects\Zen Hardware\Presentation\Tarjetas de Video.csv INTO TABLE zen_hardware.products FIELDS TERMINATED BY ',' LINES TERMINATED BY '
'"

And the error I get is this:

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 'c:usersrabajodocumentsvisual studio 2013Projectsen HardwarePresentation'

I don't know what part of my cmd syntax is wrong.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

when you are using an address that have space, must use single quotation (') before and after address like this:

"LOAD DATA INFILE 'c:usersrabajodocumentsvisual studio 2013Projectsen HardwarePresentationTarjetas de Video.csv' INTO TABLE zen_hardware.products FIELDS TERMINATED BY ',' LINES TERMINATED BY ' '"

public int Import(string path)
{
   try
   {
      string cmd = "LOAD DATA INFILE '" + path + "' INTO TABLE zen_hardware.products FIELDS TERMINATED BY ',' LINES TERMINATED BY '
'";
      int a = MySqlHelper.ExecuteNonQuery(conn.Connect(),cmd);
      return a;
   }
   catch
   {
      return -1;
   }
}

Please see the MySQL Manual Page entitled LOAD DATA INFILE Syntax.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...