conflict.aspx.cs

来自「数据库连接查询」· CS 代码 · 共 64 行

CS
64
字号
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.Linq;
using System.IO;
using System.Reflection;

public partial class Conflict : System.Web.UI.Page
{
    NorthwindDataContext ctx = new NorthwindDataContext("server=srv-devdbhost;database=Northwind;uid=sa;pwd=Abcd1234");

    protected void Page_Load(object sender, EventArgs e)
    {
        //StreamWriter sw = new StreamWriter(Server.MapPath("conflict.txt"), false);
        //ctx.Log = sw;
        //select * from products where categoryid=1

//update products set unitsinstock = unitsinstock -2, unitprice=unitprice+1 where categoryid=1

        var query = from p in ctx.Products where p.CategoryID == 1 select p;
        foreach (var p in query)
            p.UnitsInStock = Convert.ToInt16(p.UnitsInStock - 1);
        try
        {
            ctx.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
        catch (ChangeConflictException)
        {
foreach (ObjectChangeConflict cc in ctx.ChangeConflicts)
{
    Product p = (Product)cc.Object;
    foreach (MemberChangeConflict mc in cc.MemberConflicts)
    {
        string currVal = mc.CurrentValue.ToString();
        string origVal = mc.OriginalValue.ToString();
        string databaseVal = mc.DatabaseValue.ToString();
        MemberInfo mi = mc.Member;
        string memberName = mi.Name;
        Response.Write(p.ProductID + " " + mi.Name + " " + currVal + " " + origVal +" "+ databaseVal + "<br/>");
        if (memberName == "UnitsInStock")
            mc.Resolve(RefreshMode.KeepCurrentValues); // 放弃原先更新,所有更新以当前更新为准
        else if (memberName == "UnitPrice")
            mc.Resolve(RefreshMode.OverwriteCurrentValues); // 放弃当前更新,所有更新以原先更新为准
        else
            mc.Resolve(RefreshMode.KeepChanges); // 原先更新有效,冲突字段以当前更新为准

    }
}
        }
        ctx.SubmitChanges(); 

        //sw.Close();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?