0

当尝试创建一个字符串来保存以下用于将 CSV 文件导入 MySql 的查询时,查询本身会阻止我尝试创建的字符串。

string Query = "load data local infile" + " " + "'" + Filename + "'" + " " + "into table" + " " + Table + "'FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES";

我对这ENCLOSED BY '"'部分有什么要求吗?

谢谢

4

3 回答 3

0

您需要使用以下命令转义双引号字符\"\""

string Query = "load data local infile" + " " + "'" + Filename + "'" + " " + "into table" + " " + Table + "' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES";
于 2013-02-21T16:41:06.630 回答
0

这是完整的方法。

  public static void MySqlCSVImport(string Filename, string Table, string Server, string Database, string User, string Password)
        {
            try
            {
               //enclosed by '"'
                string connectionString = "server=" + Server + ";database=" + Database + ";User Id=" + User + ";password=" + Password + "";
                MySqlConnection mySqlConnection = new MySqlConnection(connectionString);
                mySqlConnection.Open();

                string Query = "load data local infile" + " " + "'" + Filename + "'" + " " + "into table" + " " + Table + "' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES";

                MySqlCommand cmd = new MySqlCommand(Query, mySqlConnection);

                cmd.ExecuteNonQuery();

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
于 2013-02-22T09:12:57.467 回答
0

如果对任何人有帮助,请通过以下方法修复该问题。

 public static void MySqlCSVImport(string Filename, string Table, string Server, string Database, string User, string Password, string Port)
        {
            try
            {
               //enclosed by '"'
                string FixFilePath = Filename.Replace(@":\", ":\\");
                string c = "'" + "\\n" + "'";
                string d = ";";

                string connectionString = "server=" + Server + ";database=" + Database + ";User Id=" + User + ";password=" + Password + "";
                MySqlConnection mySqlConnection = new MySqlConnection(connectionString);
                mySqlConnection.Open();

                string Query = "load data local infile" + " " + "'" + FixFilePath + "'" + " " + "into table" + " " + Table + " FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY" + " " + c + d;

                MySqlCommand cmd = new MySqlCommand(Query, mySqlConnection);

                cmd.ExecuteNonQuery();

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
于 2013-02-25T12:33:32.010 回答