c#
  • winforms
  • dataset
  • 2011-11-28 121 views 1 likes 
    1

    我正在處理連接到sql server的數據集,我想要做的是一次刪除多行,然後將更改保存到sql server。我用下面的代碼試了一下:將數據集更改爲數據庫

    DataRow[] rows = this.computechDataSet.VALHIS.Select("VAL_SRT = '" + this.computechDataSet.V_ALUTA.Rows[this.v_ALUTABindingSource.Position][0].ToString() + "'"); 
    foreach (DataRow row in rows) 
    { 
        row.Delete(); 
    } 
    this.computechDataSet.VALHIS.AcceptChanges(); 
    this.tableAdapterManager.UpdateAll(this.computechDataSet); 
    

    該行確實會從數據集(通過輸出數據表rowcounts找到了)刪除,但所做的更改不會保存到我的SQL數據庫。我在添加/編輯/刪除其他表上的行時沒有任何問題,除了在這些情況下我一次執行一行的操作。

    回答

    2

    嘗試沒有AcceptChanges

    DataRow[] rows = this.computechDataSet.VALHIS.Select("VAL_SRT = '" + this.computechDataSet.V_ALUTA.Rows[this.v_ALUTABindingSource.Position][0].ToString() + "'"); 
    foreach (DataRow row in rows) 
    { 
        row.Delete(); 
    } 
    this.tableAdapterManager.UpdateAll(this.computechDataSet); 
    
    0

    通過發出以下方法儘量不顯式循環的數據行。

    this.computechDataSet.VALHIS.Select("VAL_SRT = '" + this.computechDataSet.V_ALUTA.Rows[this.v_ALUTABindingSource.Position][0].ToString() + "'").Delete(); 
    
    相關問題