2011-05-16 58 views
4

我以前沒有真正使用數據集。我使用了很多LINQ/Entity Framework。如何提交這些數據行更改回數據庫

這是我寫的代碼(它是開關的一個組成部分):

if (!DataHelper.DataSourceIsEmpty(dsUp)) 
       { 
        //get datarow collection from dataset 
        DataRowCollection drc = dsUp.Tables[0].Rows; 
        //Loop through dataset 
        foreach (DataRow dr in drc) 
        { 
         //get current dataset row sortid 
         int sortID = Convert.ToInt32(dr["SortID"]); 
         { 
         //if its the row above then minus one 
         if (sortID == nodeAbove) 
         { 
          int newID = Convert.ToInt32(dr["SortID"].ToString()); 
          newID--; 
          dr["SortID"] = newID; 

          //TODO: save changes back to original ds 


         } 
         } 

        } 
       } 

       break; 

我一直在嘗試喜歡的東西:

  • dr.AcceptChanges
  • dsUp.AcceptChanges (dr)
  • drc.copyto(dsUP)
  • dsUp.Merge(drc)

和許多其他類似的嘗試沒有奏效。當谷歌搜索該主題所有的結果我發現使用表適配器...作爲即時通訊的CMS工作,我得到我的數據,像這樣:

DataSet dsUp = tree.SelectNodes(CurrentSite, path, cultureCode, true, classnames, where, orderby); 

在得到保存回數據庫的更改任何援助將得到大量讚賞 乾杯。

由於張貼我也嘗試過這種方法,不幸的是沒有工作:

 //dataset to hold results before merge 
     DataSet DSResults = tree.SelectNodes(CMSContext.CurrentSite.SiteName, path, cultureCode, true, classnames); 
     DSResults.Clear(); 


     if (!DataHelper.DataSourceIsEmpty(dsUp)) 
     { 
      //get datarow collection from dataset 
      DataRowCollection drc = dsUp.Tables[0].Rows; 
      //Loop through dataset 
      foreach (DataRow dr in drc) 
      { 
       //get current dataset row sortid 
       int sortID = Convert.ToInt32(dr["SortID"]); 
       { 
       //if its the row above then minus one 
       if (sortID == nodeAbove) 
       { 
        int newID = Convert.ToInt32(dr["SortID"].ToString()); 
        newID--; 
        dr["SortID"] = newID; 
        dr.AcceptChanges(); 
        DSResults.Tables[0].Rows.Add(dr); 



       } 
       } 

      } 
     } 
     //save changes back to original ds 
     dsUp.Merge(DSResults); 
     dsUp.AcceptChanges(); 
     break; 

回答

2

,因爲你已經退出了該數據的場景背後的數據集實現的UnitOfWork模式跟蹤您所做的所有更改來自數據庫。

你想念這裏調用數據集更新所有的更改保存回數據庫

我已經加入了更新你的代碼是什麼:

if (!DataHelper.DataSourceIsEmpty(dsUp)) 
       { 
        //get datarow collection from dataset 
        DataRowCollection drc = dsUp.Tables[0].Rows; 
        //Loop through dataset 
        foreach (DataRow dr in drc) 
        { 
         //get current dataset row sortid 
         int sortID = Convert.ToInt32(dr["SortID"]); 
         { 
         //if its the row above then minus one 
         if (sortID == nodeAbove) 
         { 
          int newID = Convert.ToInt32(dr["SortID"].ToString()); 
          newID--; 
          dr["SortID"] = newID; 
          //TODO: save changes back to original ds 

         } 
         } 

        } 
        //you can save here as the dataset will keep track of all the changes 
        YourDataAdapter.Update("tableName",dsUp) 
       }