我已经为一个数据库的表使用了模式,所以如何将所有模式从一个数据库复制到另一个数据库(在运行时创建的数据库)
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);
答案 0 :(得分:0)
使用C#,可以使用SMO
对象获得对象DDL,然后在需要将对象复制到的数据库中执行。在下面的示例中,必须引用Microsoft.SqlServer.Management.Smo,
Microsoft.SqlServer.ConnectionInfo
,Microsoft.SqlServer.Management.Sdk.Sfc
和System.Data.SqlClient
。首先从SMO对象获得DDL,然后将其用作在目标数据库中执行的CommandText
的{{1}}。此示例适用于表,但是其他对象(视图,存储过程等)也可以使用此方法进行复制。
SqlCommand