如何将所有模式从一个数据库复制到另一数据库(在运行时创建)?

时间:2018-11-13 12:54:09

标签: sql-server

我已经为一个数据库的表使用了模式,所以如何将所有模式从一个数据库复制到另一个数据库(在运行时创建的数据库)

string sql = "create database " + str1;

SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
command.ExecuteNonQuery();
Response.Write("database created");
connection.Close();


string sqll = "(select * into " + str1 + ".cost_category.cost_category_info 
              from ERPAccounting.cost_category.cost_category_info where 1=2)
              (select * into " + str1 + ".dbo.cost_centre_info from 
              ERPAccounting.cost_centre.cost_centre_info where 1=2)"

connection.Open();
SqlDataAdapter ad = new SqlDataAdapter(sqll, connection);
DataSet ds = new DataSet();
ad.Fill(ds);

1 个答案:

答案 0 :(得分:0)

使用C#,可以使用SMO对象获得对象DDL,然后在需要将对象复制到的数据库中执行。在下面的示例中,必须引用Microsoft.SqlServer.Management.Smo, Microsoft.SqlServer.ConnectionInfoMicrosoft.SqlServer.Management.Sdk.SfcSystem.Data.SqlClient。首先从SMO对象获得DDL,然后将其用作在目标数据库中执行的CommandText的{​​{1}}。此示例适用于表,但是其他对象(视图,存储过程等)也可以使用此方法进行复制。

SqlCommand