【问题标题】:How to copy a MySql database schema using C#?如何使用 C# 复制 MySql 数据库模式?
【发布时间】:2013-11-26 02:12:15
【问题描述】:

我正在尝试使用 C# 和 MySql 来复制一个空表(实际上是重新创建架构)。结构如下:

> TableTemplate (schema)
    + Tables
        > FirstTable    (table)
        > second table  (table)
        > ...

> SomeOtherTable
    + Tables
        > ... 

我想要将TableTemplate 复制到具有用户名的新架构中。

第一个被遗忘的明显途径是尝试CREATE TABLE @UserName LIKE TableTemplate,很快就知道 sql 参数应该用于值而不是表名(所有冰雹 jon skeet,再次:How to pass a table as parameter to MySqlCommand?)。

这样我们就可以手动验证用户名以构建表名 (robert's a prime example)。

接下来,似乎即使 CREATE TABLE UserID LIKE TableTemplate; 也不起作用(即使来自 MySQL Workbench),因为 TableTemplate 不是表。

因此,在创建UserID Schema 之后(在手动验证该字符串之后),或者尝试其他选项,例如转储数据库和创建一个新的,如这些问题所示:

但我宁愿避免在每次添加用户时运行进程、转储数据库并从那里创建它。

任何建议都将受到高度赞赏。

【问题讨论】:

    标签: c# mysql database-schema


    【解决方案1】:

    我认为mysqldump 会更好。但如果你想在一个过程中做。试试这个。

    SELECT
       CONCAT ("CREATE TABLE SomeOtherTable.", 
           TABLE_NAME ," AS SELECT * FROM TableTemplate.", TABLE_NAME
       ) as creation_sql
    FROM information_schema.TABLES
    WHERE TABLE_SCHEMA = 'TableTemplate';
    

    输出会是这样的

    创建表 SomeOtherTable.tbl_name AS SELECT * FROM TableTemplate.tbl_name;

    然后迭代结果并执行CREATE TABLE ....

    【讨论】:

    • 赞成,并添加了对我有用的答案。尽管如此,还是感谢您的帮助。
    【解决方案2】:

    最终使用类似这样的方法,在 aName 为表名传递的方法中:

    using (MySqlCommand cmd = new MySqlCommand(string.Format("CREATE DATABASE {0} ;", aName), connection))
    {
        cmd.ExecuteNonQuery();          // Create the database with the given user name
    
        // Building the sql query that will return a "create table" per table in some_db template DB.
        cmd.CommandText = (string.Format("SELECT CONCAT (\"CREATE TABLE {0}.\", TABLE_NAME ,\" "
                                           + "LIKE some_other_db.\", TABLE_NAME ) as creation_sql "
                                           + "FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'some_db';"
                                        , aName));
    
        try     // Building the inner tables "create table" sql strings
        {
            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                    createInnerTablesList.Add(reader.GetString(0));
            }
        }
        catch (MySqlException mysql_ex) { ... } // handle errors 
    
        foreach (var sql_insert_query in createInnerTablesList)
        {
            try                         // Insert the tables into the user database
            {
                cmd.CommandText = sql_insert_query;
                cmd.ExecuteNonQuery();
            }
            catch (Exception e) { ... } // handle errors 
        }
    } 
    

    像 Jungsu 建议的那样使用LIKE vs AS 的原因是,即使AS 将创建表,它也不会保留任何已定义的约束和键(主键等)。

    使用LIKE 将使用约束复制它们。

    我仍然对此不太高兴,因为我觉得我错过了一些东西......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2016-10-25
      • 2010-11-21
      • 1970-01-01
      • 2014-04-21
      相关资源
      最近更新 更多