简体   繁体   中英

How to get all records from one table and save it on another table by c# in winforms?

Hello guys, i am trying to get all records from tblInvoiceItemsTemp table and save all the records inside the tblInvoiceItems table but not able to solve. Any help would be appreciated, thank you.

I have added following code on btnSave_Click() event.

string connetionString1 = "server=localhost;database=billingDB;uid=root;pwd=root;integrated security=true";
using (MySqlConnection cnn1 = new MySqlConnection(connetionString1))
{
    cnn1.Open();
    string load_temp_table_rec_qry = "SELECT * FROM tblInvoiceItemsTemp";
    using (MySqlCommand sqlcmd = new MySqlCommand(load_temp_table_rec_qry, cnn1))
    {
        MySqlDataReader temp_reader = sqlcmd.ExecuteReader();
        while (temp_reader.Read())
        {
            string insert_invoice_items_qry = "INSERT INTO tblInvoiceItems(invoiceID, particulars, qty, rate) VALUES('" + 12 + "', '" + temp_reader["particulars"] + "', '" + temp_reader["qty"] + "', '" + temp_reader["rate"] + "')";
            using (MySqlCommand itemsCmd = new MySqlCommand(insert_invoice_items_qry, cnn1))
            {
                itemsCmd.ExecuteNonQuery();
            }
        }
    }
    cnn1.Close();
}

I am getting following error messages.

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
Additional Information: There is already an open DataReader associated with this Connection which must be closed first.

The error message is pretty clear: while you have a DataReader open (you haven't called Close / Dispose ), the Connection cannot be used for anything else. One way to do this is to open a second connection:

using (MySqlCommand sqlcmd = new MySqlCommand(load_temp_table_rec_qry, cnn1))
{
    MySqlDataReader temp_reader = sqlcmd.ExecuteReader();
    using (var secondConnection = new MySqlConnection(connetionString1))
    {
        secondConnection.Open();
        while (temp_reader.Read())
        {
            string insert_invoice_items_qry = "INSERT INTO tblInvoiceItems(invoiceID, particulars, qty, rate) VALUES('" + 12 + "', '" + temp_reader["particulars"] + "', '" + temp_reader["qty"] + "', '" + temp_reader["rate"] + "')";
            using (MySqlCommand itemsCmd = new MySqlCommand(insert_invoice_items_qry, secondConnection))
            {
                itemsCmd.ExecuteNonQuery();
            }
        }
    }
}

Another way is to use the disconnected model and load the records to a DataTable using a MySqlDataAdapter , so that the connection is free for using for itemsCmd .
However, you don't need to download into memory all the records for this, you can do an INSERT INTO SELECT , for much better performance:

INSERT INTO tblInvoiceItems(invoiceID, particulars, qty, rate)
SELECT 12, tblInvoiceItemsTemp.particulars, tblInvoiceItemsTemp.qty, tblInvoiceItemsTemp.rate
FROM tblInvoiceItemsTemp

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