简体   繁体   English

通过LINQ在不同的数据库中更新

[英]Updating through LINQ in different DataBase

I have a DB which looks like this: 我有一个看起来像这样的数据库:

1st: 第一:

CommissionsV2 (Table = Entity_Product_Point) 佣金V2(表格= Entity_Product_Point)
This is my DBML too which has only one table. 这也是我的DBML,它只有一个表。

2nd: 第二名:

WebEnroll (Table = PlanMaster) WebEnroll(表格= PlanMaster)
This is my another DBML which has one table in it. 这是我的另一个DBML,其中有一个表。

Now through LINQ I am adding a row in this which has a query like this: 现在,通过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();

Now my update statement is like this: 现在我的更新语句是这样的:

 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();

In order to update, you need to retrieve the entity that needs to be updated. 为了更新,您需要检索需要更新的实体。

So in your case it would be: 因此,在您的情况下,它将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM