sql查询字符串中的双引号

时间:2013-02-21 16:38:38

标签: mysql string

当尝试创建一个字符串来保存以下查询以将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 '"'部分有什么看法吗?

由于

3 个答案:

答案 0 :(得分:0)

您需要使用\"\""

来转义双引号字符
string Query = "load data local infile" + " " + "'" + Filename + "'" + " " + "into table" + " " + Table + "' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES";

答案 1 :(得分: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);
            }
        }

答案 2 :(得分: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);
            }
        }