简体   繁体   English

如何使用WinForms中的C#从一个表中获取所有记录并将其保存到另一表中?

[英]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. 大家好,我试图从tblInvoiceItemsTemp表中获取所有记录,并将所有记录保存在tblInvoiceItems表中,但无法解决。 Any help would be appreciated, thank you. 任何帮助,将不胜感激,谢谢。

I have added following code on btnSave_Click() event. 我在btnSave_Click()事件上添加了以下代码。

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. 错误消息非常清楚:打开DataReader时(尚未调用Close / Dispose ),Connection不能用于其他任何用途。 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 . 另一种方法是使用断开连接的模型,并使用MySqlDataAdapter将记录加载到DataTable ,以便该连接可以免费用于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 SELECT以获得更好的性能:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个表中选择记录,检查它是否在另一个表中,然后插入C#中的第三个表中 - Select records from one table, check if it exists in another table then insert into a 3rd table in C# 如何在C#中基于表格的一列显示记录? - how to display records based on one column from table in c#? 需要从一个表中获取所有记录,这些记录匹配从另一个表返回的值 - Need to get all records from one table, that match values I return from another table 如何将记录从一张表更新到另一张表 - how to update the records from one table to another 如何从一个表中获取所有记录并基于每个记录获取与 mvc 5 中第一个表记录关联的所有记录 - how to get all records from one table and based on that each record fetch all records which associated to the first table records in mvc 5 将一个表列中的所有记录保存在数组中 - Save all records from one table column in an array 如何在C#Winforms中使用MVP模式将多个表中的数据显示到我的视图中? - How to display data from more than one table to my view using MVP pattern in C# winforms? 如何在WinForms C#中将数据从一种形式传输到另一种形式? - How to transfer data from one form to another in winforms c#? 如何将数据从一个表移动到另一个C# - how to Move data from one table to another c# 数据表 C# - 如何根据 ID 从一个表中获取详细记录及其从另一表中的关联子记录 - Datatable C# - How to get detail record from one table and its associated sub record from another table based on ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM