2017-10-13 30 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

相關問題