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

发布于 2024-11-01 01:38:20 字数 2707 浏览 1 评论 0原文

我有一个如下所示的数据库:

第一:

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

I have a DB which looks like this:

1st:

CommissionsV2 (Table = Entity_Product_Point)
This is my DBML too which has only one table.

2nd:

WebEnroll (Table = PlanMaster)
This is my another DBML which has one table in it.

Now through LINQ I am adding a row in this which has a query like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀梦 2024-11-08 01:38:20

为了更新,您需要检索需要更新的实体。

所以在你的情况下它将是:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文