简体   繁体   English

如何将行从一个数据库中的表移到另一个数据库?

[英]How do I move a row from a table in one database to another?

So in my program, I have two database. 因此在我的程序中,我有两个数据库。 One is 'CurrentCargo', the other is 'PastOrders' 一个是“ CurrentCargo”,另一个是“ PastOrders”

What I wanted to do is that when the tick box in "CurrentCargo"'s table had been ticked, it will take the variables in the row and move it into the "PastOrders"'s Table. 我想做的是,当在“ CurrentCargo”表中的复选框打勾时,它将采用该行中的变量并将其移到“ PastOrders”表中。 As shown in the image below: Tick in tick box triggers a movement of row values So this is what I typed for my code: 如下图所示: 勾选复选框会触发行值的移动,所以这是我为代码键入的内容:

private void CurrentCargoTable_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 9 && e.RowIndex >= 0)
        {
            bool Check = Convert.ToBoolean(CurrentCargoTable.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);

            int Index = 0;

            int Code;
            DateTime ImportDate;
            DateTime ExportDate;
            string From;
            string To;
            string TransportMode;
            string Supplier;
            Decimal Cost;
            bool Payed;
            bool Delivered;

            if (Check == true)
            {
                Index = CurrentCargoTable.CurrentCell.RowIndex;

                Code = (int)CurrentCargoTable.Rows[Index].Cells[1].Value;
                ImportDate = (DateTime)CurrentCargoTable.Rows[Index].Cells[2].Value;
                ExportDate = (DateTime)CurrentCargoTable.Rows[Index].Cells[3].Value;
                From = (String)CurrentCargoTable.Rows[Index].Cells[4].Value;
                To = (String)CurrentCargoTable.Rows[Index].Cells[5].Value;
                TransportMode = (String)CurrentCargoTable.Rows[Index].Cells[6].Value;
                Supplier = (String)CurrentCargoTable.Rows[Index].Cells[7].Value;
                Cost = (Decimal)CurrentCargoTable.Rows[Index].Cells[8].Value;
                Payed = false;
                Delivered = false;


                string sql = string.Format("insert into dataGridView1 (Code, Import_Date, Export_Date, From, To, TransportMode, Supplier, Cost, Payed, Delivered) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}');", Code, ImportDate, ExportDate, From, To, TransportMode, Supplier, Cost, Payed, Delivered);
                this.tableTableAdapter1.Insert(sql);
                this.tableAdapterManager1.UpdateAll(this.pastOrdersDataSet);
                dataGridView1.Update();

But it still doesn't work. 但这仍然行不通。 I tried to use a message box to see if the variables are storing the right values. 我试图使用一个消息框来查看变量是否存储了正确的值。 And every single one is holding the value I wanted. 每个人都拥有我想要的价值。 So I kind of get the part of "reading", but I don't get the part of "writing". 所以我有点“阅读”的一部分,但我没有“写作”的一部分。

Please help me if anyone knows, thank you. 如果有人知道,请帮助我,谢谢。

The following will will work if there is no previous data (may be you need to check for this before executing) 如果没有先前的数据,则以下内容将起作用(可能是您需要在执行之前进行检查)

string sql = string.Format(@"Select * into CurrentCargo  from PastOrders Where 
PastOrders.Code = '{0}'", Code); 

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

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