如何在没有循环的情况下获取所有表名和模式

时间:2013-12-26 12:20:43

标签: c# mysql

我必须检索旧表的名称和模式,以便在程序中的新数据库中创建新表。我写了这个有效的程序。在第一部分中,我获取表的名称并将其存储在MyArray中,在第二部分中,我将这些名称放在查询字符串中。但在第二部分中,我首先读取了表的名称,以下数据是模式。

是否有这样的解决方案:(这不起作用我尝试过:)

MySqlCommand cmd = new MySqlCommand("SHOW * CREATE TABLE " + DbName, conn);

我的功能在那里:

    private int CopySchemas(string pathname)
    {
        string [] MyArray = new string[11];
        int index = 0;
        using (MySqlConnection conn = new MySqlConnection(connstr))
        {
            using(MySqlCommand cmd = new MySqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='" + DbName+"'",conn))
            {
                    conn.Open();
                    try
                    {
                        MySqlDataReader reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            MyArray[index] = reader.GetValue(0).ToString();
                            ++index;
                        }
                        reader.Dispose();
                    }
                    catch (MySqlException e)
                    {
                        MessageBox.Show(e.Number.ToString() + " -> " + e.Message.ToString());
                        return 0;
                    }
              }
            for (int i = 0; i < MyArray.Length; ++i)
            {
                string tblname = MyArray[i];
                string fname = pathname  +"\\" + tblname + ".sql";
                MySqlCommand cmd = new MySqlCommand("SHOW CREATE TABLE " + DbName + "." + tblname, conn);
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string fname = pathname  +"\\" + reader.GetValues(0) + ".sql";
                    string schema = reader.GetValue(1).ToString();
                    File.WriteAllText(fname,schema);
                }
                reader.Dispose();
            }
        }
        return index;
    }

1 个答案:

答案 0 :(得分:0)

最后我看到,每个连接必须只有一个阅读器。

所以我打开了两个连接,每个连接我都联系了一个阅读器。所以我不使用数组来存储表名,我只使用第二个读者来获取表名和模式。

以下是代码:

    private int CopySchemas(string pathname)
    {
        int index = 0;
        MySqlConnection conn1 = new MySqlConnection(PublicVariables.cs);
        using (MySqlConnection conn = new MySqlConnection(PublicVariables.cs))
        {
            using (MySqlCommand cmd = new MySqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='" + PublicVariables.DbName + "'", conn))
            {
                conn.Open();
                conn1.Open();
                try
                {
                    MySqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        string tblname = reader.GetValue(0).ToString();
                        MySqlCommand cmd1 = new MySqlCommand("SHOW CREATE TABLE " + PublicVariables.DbName + "." + tblname, conn1);
                        MySqlDataReader reader1 = cmd1.ExecuteReader();
                        while (reader1.Read())
                        {
                            string fname = pathname + "\\" + reader1.GetValue(0).ToString() + ".sql";
                            string schema = reader1.GetValue(1).ToString();
                            File.WriteAllText(fname, schema);
                        }
                        reader1.Dispose();
                        ++index;
                    }
                }
                catch (MySqlException e)
                {
                    MessageBox.Show(e.Number.ToString() + " -> " + e.Message.ToString());
                    return 0;
                }
            }
        }
        return index;
     }