简体   繁体   English

MySqlBackup.NET QueryExpress.ExecuteScalarStr()输出用双引号而不是反引号引起来的字符串

[英]MySqlBackup.NET QueryExpress.ExecuteScalarStr() outputs strings enclosed in double-quotes instead of backticks

I'm using MySqlBackup.dll (MySqlBackup.NET) which in turn uses MySql.Data.dll to dump the database. 我正在使用MySqlBackup.dll(MySqlBackup.NET),后者又使用MySql.Data.dll转储数据库。 I thought MySqlBackup.NET was causing this behavior, so I took it out of the equation. 我以为MySqlBackup.NET导致了这种现象,所以我将其排除在外。 If I run this code in my solution: 如果我在解决方案中运行此代码:

Dim cmd = New MySqlCommand()
        cmd.Connection = New MySqlConnection(connectionString)
        cmd.Connection.Open()
        Dim result = QueryExpress.ExecuteScalarStr(cmd, "SHOW CREATE TABLE `airportcodes`;", 1)
        cmd.Connection.Close()

I get 我懂了

CREATE TABLE "airportcodes" (
  "AirportCodeId" int(11) NOT NULL AUTO_INCREMENT,
  "Code" varchar(4) CHARACTER SET utf8 NOT NULL,
  "AirportName" varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  "Website" varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  "LastUpdate" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY ("AirportCodeId")
)

which I cannot use to restore because it uses double quotes. 我无法使用它恢复,因为它使用双引号。 This happens when I use both the code above and MySqlBackup.NET. 当我同时使用上面的代码和MySqlBackup.NET时,会发生这种情况。 If I use the MySqlBackup.NET test application provided with its source code, the result is correct (uses backticks instead of double-quotes). 如果我使用其源代码随附的MySqlBackup.NET测试应用程序,则结果是正确的(使用反引号而不是双引号)。

If I execute this query in the mysql CLI I also get the correct version (with backticks). 如果我在mysql CLI中执行此查询,我还将获得正确的版本(带有反引号)。 I am using the same connection string all over. 我到处都使用相同的连接字符串。

It feels stupid to search-and-replace after the dump is created. 创建转储后,进行搜索和替换感觉很愚蠢。 What could be the cause of this? 这可能是什么原因?

This has nothing to do with MySqlBackup.NET 这与MySqlBackup.NET无关

It is the behavior of MySql Server which can be configured either as default (GLOBAL) behavior or temporary (SESSION) behavior of MySQL server. MySql Server的行为可以配置为MySQL服务器的默认(GLOBAL)行为或临时(SESSION)行为。

Here is the SQL statements that you can try out yourself: 您可以尝试以下SQL语句:

Example 1: Export table structure with double quotes: 示例1:导出带有双引号的表结构:

set session sql_mode=ANSI_QUOTES;
show create table `configkey`;

output: 输出:

CREATE TABLE "configkey" (
  "key" varchar(100) NOT NULL,
  "value" text,
  PRIMARY KEY ("key")
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Example 2: Export table structure with single quote 示例2:导出单引号的表结构

set session sql_mode=traditional;
show create table `configkey`;

output: 输出:

CREATE TABLE `configkey` (
  `key` varchar(100) NOT NULL,
  `value` text,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

For more information, please refer to official MySQL documentation under the topic: SQL-MODE https://dev.mysql.com/doc/refman/5.7/en/server-options.html#option_mysqld_sql-mode https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html 有关更多信息,请参考以下主题的官方MySQL文档:SQL-MODE https://dev.mysql.com/doc/refman/5.7/en/server-options.html#option_mysqld_sql-mode https:// dev。 mysql.com/doc/refman/5.7/en/sql-mode.html

If you see the column names are wrapped with double quotes by default, it's most probably your MySQL server is configured to react in that way by default. 如果您看到默认情况下列名用双引号引起来,则很可能您的MySQL服务器默认配置为以这种方式做出反应。 You may consult your MySQL server administrator or provider. 您可以咨询您的MySQL服务器管理员或提供商。

Alternatively, you can configure the SQL_MODE each time manually before executing MySqlBackup.NET. 或者,您可以在执行MySqlBackup.NET之前每次手动配置SQL_MODE。 Below is an example: 下面是一个示例:

using (MySqlConnection conn = new MySqlConnection(constring))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();

            cmd.CommandText = "set session sql_mode=traditional;";
            cmd.ExecuteNonQuery();

            mb.ExportToFile(file);
            conn.Close();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM