简体   繁体   中英

copy all rows of a table to another table

I have two databases in MySQL and SQL Server , and I want to create tables in SQL Server and copy all rows from the table in MySQL into the new table in SQL Server .

I can create table in SQL Server same as MySQL , with this code:

List<String> TableNames = new List<string>();
{ 
    IDataReader reader= 
        ExecuteReader("SELECT Table_Name FROM information_schema.tables WHERE table_name LIKE 'mavara%'",MySql);
    while (reader.Read()) {
        TableNames.Add(reader[0].ToString());
    }
    reader.Close();
}

foreach (string TableName in TableNames) {
    IDataReader reader = 
        ExecuteReader("SELECT Column_Name,IS_NULLABLE,DATA_TYPE FROM information_schema.columns where TABLE_Name='" + TableName + "'",MySql);
    List<string[]> Columns = new List<string[]>();
    while (reader.Read()) { 
        string[] column = new string[3];
        column[0] = reader[0].ToString();
        column[1] = reader[1].ToString();
        column[2] = reader[2].ToString();
        Columns.Add(column);
    }

    reader.Close();

    // create table
    string  queryCreatTables=  "CREATE TABLE [dbo].[" + TableName + "](\n";
    foreach(string[] cols in Columns)
    {
        queryCreatTables +="["+ cols[0] + "] " + cols[2] + " ";
        if (cols[1] == "NO")
            queryCreatTables += "NOT NULL";
        // else
        //   queryCreatTables += "NULL";
        queryCreatTables += " ,\n ";
    }
    queryCreatTables += ")";

    System.Data.SqlClient.SqlCommand smd = 
        new System.Data.SqlClient.SqlCommand(queryCreatTables, MsSql);
    System.Data.SqlClient.SqlDataReader sreader = smd.ExecuteReader();
    sreader.Close();

but I have problem to copy rows from one table into another table.

for select query, I use Idatareader, but I don't know how insert rows to another table.

For inserting rows from one table into another table please refer the below sample query

    INSERT INTO Store_Information (store_name, Sales, Date)
    SELECT store_name, sum(Sales), Date
     FROM Sales_Information

The algorithm is as follows:

1. For each table in source database
2.    Get a list of columns for that table
3.    Create table in destination database
4.    SELECT * FROM the table in source
5.    For each row in data
6.       Generate INSERT statement and execute on destination database

The information you need for a column is Name , Type , Length , etc.

Then you generate the insert statement by iterating on the columns

var insertStatement = "INSERT INTO " + tableName + " VALUES( ";
foreach( var column in columns )
    insertStatement += "@" + column.Name + ",";
insertStatement[insertStatement.Length-1] = ')';

var command = new SqlCommand( insertStatement, MsSql );

// iterate over the columns again, but this time set values to the parameters
foreach( var column in columns )
   command.Parameters.AddWithValue( "@"+column.Name, currentRow[column.Name] );

But I have a problem to copy rows from one table into another table.

You can use the SqlDataAdapter.UpdateBatchSize to perform Batch Updates/Inserts with a DataAdapter against the database to copy the data from one table to the other. After you get all records from the first MYSQL table using something like:

//Get the rows from the first mysql table as you did in your question
DataTable mysqlfirstTableRowsTobeCopied = GetDataTableFromMySQLTable();

Then you have to create a commend text that do the INSERT something like:

cmd.CommandText = "INSERT INTO TableName Column_Name, ... VALUES(...)";

Or you can use a stored procedure:

CREATE PROCEDURE sp_BatchInsert ( @ColumnName VARCHAR(20), ... ) 
AS 
BEGIN 
            INSERT INTO TableName VALUES ( @ColumnNamne, ...); 
END

Then:

DataTable mysqlfirstTableRowsTobeCopied = GetDataTableFromMySQLTable();

SqlConnection conn = new SqlConnection("Your connection String");
SqlCommand cmd = new SqlCommand("sp_BatchInsert", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.UpdatedRowSource = UpdateRowSource.None;

// Set the Parameter with appropriate Source Column Name
cmd.Parameters.Add("@ColumnName", SqlDbType.Varchar, 50,
    mysqlfirstTableRowsTobeCopied.Columns[0].ColumnName);   
...
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.InsertCommand = cmd;
// Specify the number of records to be Inserted/Updated in one go. Default is 1.
adpt.UpdateBatchSize = 20;

conn.Open();
int recordsInserted = adpt.Update(mysqlfirstTableRowsTobeCopied);   
conn.Close();

This code actually is quoted from a full tutorial about this subject in the codeproject, you can refer to it for more information:

Assuming that you already have a datatable with rows that you want to merge with another table...

There are two ways to do this...again, assuming you've already selected the data and put it into a new table.

            oldTable.Load(newTable.CreateDataReader());

            oldTable.Merge(newTable);

Usually that's sth. you can do in SQL directly: INSERT INTO table FROM SELECT * FROM othertable ;

For insertion of all record into new table(If the second table is not exist)

Select * into New_Table_Name from Old_Table_Name;

For insertion of all records into Second Table(If second table is exist But the table structure should be same)

Insert into Second_Table_Name from(Select * from First_Table_Name);

Just in case it can help someone, I've found an easy way to do it in C# using SqlDataAdapter. It automatically detects the structure of the table so you don't have to enumerate all the columns or worry about table structure changing. Then bulk inserts the data in the destination table.

As long as the two tables have the same structure, this will work. (For example, copying from a production table to a dev empty table.)

using(SqlConnection sqlConn = new SqlConnection(connectionStringFromDatabase))
    {
      sqlConn.Open();
      using(var adapter = new SqlDataAdapter(String.Format("SELECT * FROM [{0}].[{1}]", schemaName, tableName), sqlConn))
      {
        adapter.Fill(table);
      };
    }

    using(SqlConnection sqlConn = new SqlConnection(connectionStringDestination))
    {
      sqlConn.Open();
      // perform bulk insert
      using(var bulk = new SqlBulkCopy(sqlConn, SqlBulkCopyOptions.KeepIdentity|SqlBulkCopyOptions.KeepNulls, null))
      {
        foreach(DataColumn column in table.Columns)
        {
          bulk.ColumnMappings.Add(column.ColumnName, column.ColumnName);
        }
        bulk.DestinationTableName = String.Format("[{0}].[{1}]", schemaName, tableName);
        bulk.WriteToServer(table);
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM