2011-04-11 17 views
0

我有一個數據庫,它看起來像這樣:更新通過LINQ在不同的數據庫

1:

CommissionsV2(表= Entity_Product_Point)
這是我的DBML過它只有一個表。

第二:

WebEnroll(表= PlanMaster)
這是我的另一個DBML具有一個表中它。

現在通過LINQ我加入這一行其中有這樣的查詢:

     CommissionsV2DataContext cv = new CommissionsV2DataContext(); 
         Entity_Product_Point ev = new Entity_Product_Point(); 
         ev.Entity_ID = getEntity; 
         ev.Product_ID = tr.First(); 
         ev.HiCommissionOld = (double)firststYrComp; 
         ev.LowCommissionOld = (double)recurringComp; 
         ev.HiCommission = (double)finalFirstYrComp * 100; 
         ev.LowCommission = (double)finalRecurringComp * 100; 
         ev.DateCreated = System.DateTime.Now; 

         cv.Entity_Product_Points.InsertOnSubmit(ev); 
         cv.SubmitChanges(); 

現在我的更新語句是這樣的:

protected void gvShowComm_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 

     //Getting the Entity_ID from the Session! 
     int getEntity = Int16.Parse(Session["EntitySelected"].ToString()); 

     //Accessing the variables from the controls! 
     System.Web.UI.WebControls.TextBox product_ID = gvShowComm.Rows[e.RowIndex].FindControl("ProductName") as System.Web.UI.WebControls.TextBox; 
     System.Web.UI.WebControls.TextBox planName = gvShowComm.Rows[e.RowIndex].FindControl("PlanName") as System.Web.UI.WebControls.TextBox; 
     System.Web.UI.WebControls.TextBox hiCommOld = gvShowComm.Rows[e.RowIndex].FindControl("HiComm") as System.Web.UI.WebControls.TextBox; 
     System.Web.UI.WebControls.TextBox lowCommOld = gvShowComm.Rows[e.RowIndex].FindControl("LowComm") as System.Web.UI.WebControls.TextBox; 

     //Storing the values into variables! 
     int product = Int16.Parse(product_ID.Text); 
     string plan = planName.Text; 
     int hiOld = Int16.Parse(hiCommOld.Text); 
     int lowOld = Int16.Parse(lowCommOld.Text); 

     //Updating the values into the table through LINQ! 
     dbWebEnrollDataContext dt = new dbWebEnrollDataContext(); //This has PlanName in PlanMaster Table. 
     CommissionsV2DataContext cv = new CommissionsV2DataContext(); //Entity_Product_Point has all the other columns which needs to be updated! 

     Entity_Product_Point ev = cv.Entity_Product_Points.Single(c => c.Product_ID == product); 
     ev.HiCommissionOld = hiOld; 
     ev.LowCommissionOld = lowOld; 
     ev.Entity_ID = getEntity; 
     cv.SubmitChanges(); 

回答

0

爲了更新,您需要檢索需要更新的實體。

所以你的情況這將是:

Entity_Product_Point ev = cv.Entity_Product_Points.Single(c => c.Product_ID == product); 
ev.HiCommissionOld = hiOld; 
ev.LowCommissionOld = lowOld; 
// Retrieve the plan that needs to be updated and set the name 
// Submit the changes 
cv.SubmitChanges(); 
// Retrieve the new values and rebind the gridview against the new values 
+0

@Daven:我做了一些改變,以我的問題。請看一看。實際上,我正在GView Databind中提取屬於該特定實體的產品。所以在技術上,我得到屬於該特定Entity_ID的所有產品。 – 2011-04-11 16:08:09

+0

@ DOT-NET-SLUT:它仍然是一個問題,您需要檢索需要更新的產品,更改這些值並在相關的數據上下文中調用.SubmitCHanges()。試試看看會發生什麼。 – 2011-04-11 16:13:12

+0

@Daven:好的!讓我試試。 – 2011-04-11 16:16:41