C# 使用C动态地将所有表的数据从一个SQL Server数据库插入到第二个数据库中#
我用过这个代码。有人能帮我把数据从一个数据库转移到另一个数据库吗C# 使用C动态地将所有表的数据从一个SQL Server数据库插入到第二个数据库中#,c#,sql-server,c#-4.0,c#-3.0,C#,Sql Server,C# 4.0,C# 3.0,我用过这个代码。有人能帮我把数据从一个数据库转移到另一个数据库吗 SqlConnection SourceServerName = new SqlConnection(@"Data Source = Stack; Initial Catalog = SSIS2;Trusted_Connection=yes;"); SqlConnection DestinationServerName = new SqlConnection(@"Data Source = Stack; Initial Catal
SqlConnection SourceServerName = new SqlConnection(@"Data Source = Stack; Initial Catalog = SSIS2;Trusted_Connection=yes;");
SqlConnection DestinationServerName = new SqlConnection(@"Data Source = Stack; Initial Catalog = SSIS1;Trusted_Connection=yes;");
SqlCommand Cmd = new SqlCommand("SELECT NAME FROM sys.TABLES", SourceServerName);
SourceServerName.Open();
System.Data.SqlClient.SqlDataReader reader = Cmd.ExecuteReader();
while(reader.Read())
{
Cmd = new SqlCommand("TRUNCATE TABLE " + reader["name"], DestinationServerName);
DestinationServerName.Open();
Cmd.ExecuteNonQuery();
reader = Cmd.ExecuteReader();
SqlBulkCopy bulkData = new SqlBulkCopy(DestinationServerName);
// String Dest = reader["name"].ToString();
bulkData.DestinationTableName = reader["name"].ToString();
bulkData.WriteToServer(reader);//reader);
bulkData.Close();
DestinationServerName.Close();
}
SourceServerName.Close();
您不能像以前那样重用DataReader和SqlCommand。重复使用连接也会让人头疼,但由于您没有分享创建这些连接的方式或地点,我暂时没有提及
// consider wrapping in a using as well
SqlCommand Cmd = new SqlCommand("SELECT NAME FROM sys.TABLES", SourceServerName);
SourceServerName.Open();
System.Data.SqlClient.SqlDataReader reader = Cmd.ExecuteReader();
while(reader.Read())
{
// create a new command to truncate the table at the destination
using(var TruncateCmd = new SqlCommand("TRUNCATE TABLE " + reader["name"], DestinationServerName))
{
DestinationServerName.Open();
TruncateCmd.ExecuteNonQuery();
}
// sqlbulkcopy is IDisposable, wrap in a using
using(var SqlBulkCopy bulkData = new SqlBulkCopy(DestinationServerName))
{
// have a new SourceCmd to get a DataReader for the source table
// create a new connection, just to be sure
using(var SourceCon = new SqlConnection(SourceServerName.ConnectionString))
using(var SourceCmd = new SqlCommand("select * FROM " + reader["name"], SourceCon))
{
SourceCon.Open(); // this is definitely needed
DestinationServerName.Open(); // not 100% sure if this is needed
bulkData.DestinationTableName = reader["name"].ToString();
// WriterToServer knows how to deal with a DataReader
bulkData.WriteToServer(SourceCmd.ExecuteReader());
}
}
}
您不能像以前那样重用DataReader和SqlCommand。重复使用连接也会让人头疼,但由于您没有分享创建这些连接的方式或地点,我暂时没有提及
// consider wrapping in a using as well
SqlCommand Cmd = new SqlCommand("SELECT NAME FROM sys.TABLES", SourceServerName);
SourceServerName.Open();
System.Data.SqlClient.SqlDataReader reader = Cmd.ExecuteReader();
while(reader.Read())
{
// create a new command to truncate the table at the destination
using(var TruncateCmd = new SqlCommand("TRUNCATE TABLE " + reader["name"], DestinationServerName))
{
DestinationServerName.Open();
TruncateCmd.ExecuteNonQuery();
}
// sqlbulkcopy is IDisposable, wrap in a using
using(var SqlBulkCopy bulkData = new SqlBulkCopy(DestinationServerName))
{
// have a new SourceCmd to get a DataReader for the source table
// create a new connection, just to be sure
using(var SourceCon = new SqlConnection(SourceServerName.ConnectionString))
using(var SourceCmd = new SqlCommand("select * FROM " + reader["name"], SourceCon))
{
SourceCon.Open(); // this is definitely needed
DestinationServerName.Open(); // not 100% sure if this is needed
bulkData.DestinationTableName = reader["name"].ToString();
// WriterToServer knows how to deal with a DataReader
bulkData.WriteToServer(SourceCmd.ExecuteReader());
}
}
}
不需要该内部循环,当然也不应该在第一个复制的表之后关闭源连接。不需要该内部循环,当然也不应该在第一个复制的表之后关闭源连接。