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

时间:2017-10-13 03:38:35

标签: c# database visual-studio datagridview

所以在我的程序中,我有两个数据库。 一个是'CurrentCargo',另一个是'PastOrders'

我想要做的是,当勾选“CurrentCargo”表格中的勾选框时,它将获取行中的变量并将其移动到“PastOrders”的表中。 如下图所示:  Tick in tick box triggers a movement of row values 所以这就是我为我的代码输入的内容:

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();

但它仍然无效。我尝试使用消息框来查看变量是否存储了正确的值。每一个人都拥有我想要的价值。所以我得到了“阅读”的一部分,但我没有得到“写作”的一部分。

如果有人知道,请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:-1)

如果没有以前的数据,可能会有以下情况(可能需要在执行前检查)

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