0

I have used this code. Can anyone help me on the code part to move the data one database to another?

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();
1
  • 1
    There is no need of that internal loop and of course you shouldn't close the source connection after the first copied table. Commented May 12, 2018 at 8:39

1 Answer 1

2

You can't reuse the DataReader and SqlCommand like you do. Also reusing a connection can give headaches but as you didn't share how or where you created those I left that untouched for now.

// 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());
        } 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.