2017-10-13 65 views
0

所以在我的程序中,我有两个数据库。 其中一个是'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(); 

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

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

+0

不要用表值工作。处理底层数据。如果你的基础数据是'List ',那么很简单 - 'Db2Prsister.Insert(myList [idx]); Db1Prsister.Delete(myList中[IDX]);'。您只是有效地将记录从一个数据库移到另一个数据库当然,EF会帮助开发这个 –

回答

-1

如果没有以前的数据下面的意愿就可以了(可能是你需要在执行前检查此)

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

@LignerPoi的速度,请注意,如果您想从“CurrentCargo”中删除该行,则必须运行额外的'DELETE'语句。 –

+0

你的代码似乎工作...一半? ............红线在那些不在字符串中的行下,例如DateTime中的ImportDate和ExportDate。如果我尝试使用“Convert.ToDateTime()”将其转换为DateTime,则不会再有红线。但是,当我运行调试时,它会弹出“在mscorlib.dll中发生类型'System.FormatException'的异常,但没有在用户代码中处理” – LingnerPoi

相关问题