使用c#使用其他数据库架构信息生成数据库

时间:2012-09-06 06:02:59

标签: c# mysql sql-server

我想使用c#

从其他数据库架构信息生成新的sql数据库

以下代码显示了从数据库中检索信息

        string connetionString = null;

        SqlConnection connection;

        SqlCommand command;

        SqlDataAdapter adapter = new SqlDataAdapter();

        DataSet ds = new DataSet();

        int i = 0;

        string sql = null;



        connetionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

        sql = "Select DISTINCT(name) FROM sys.Tables";



        connection = new SqlConnection(connetionString);



        try
        {

            connection.Open();

            command = new SqlCommand(sql, connection);

            adapter.SelectCommand = command;

            adapter.Fill(ds);

            adapter.Dispose();

            command.Dispose();

            connection.Close();

            DataSet dset = new DataSet();

            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {

                listBox1.Items.Add(ds.Tables[0].Rows[i].ItemArray[0].ToString());
                SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM "+ds.Tables[0].Rows[i].ItemArray[0].ToString()+"",connection);

                ad.Fill(dset);


            }
            dset.Tables[0].WriteXml(@"E:\WindowsFormsApplication2\WindowsFormsApplication2\ds.xml");

        }

        catch (Exception ex)
        {

            MessageBox.Show("Can not open connection ! ");

        }

同样明智的是,我必须从数据库中获取信息,并且必须使用此架构信息生成新的数据库。

1 个答案:

答案 0 :(得分:0)

将架构从一个数据库复制到另一个数据库:

  1. 使用INFORMATION_SCHEMA表(例如INFORMATION_SCHEMA.TABLES)遍历源数据库中的对象以获取表的列表。
  2. 对于SQL Server,请在每个(或sp_help存储过程)上调用sp_helptext存储过程。对于MySQL,请使用SHOW CREATE TABLESHOW CREATE PROCEDURE和类似命令。这些返回用于创建对象的SQL。
  3. 在目标数据库上运行该SQL。
  4. 请注意,您可以通过SQL Server Management Studio生成用于创建SQL Server数据库的SQL脚本。