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 に答える