• Linq to sql(九):其它补充(三)


    撤销提交

            var customer = ctx.Customers.Single(c => c.CustomerID == "AROUT");

            customer.ContactName = "zhuye";

            customer.Country = "Shanghai";

            Response.Write(string.Format("Name:{0},Country:{1}<br/>", customer.ContactName, customer.Country));

            customer = ctx.Customers.GetOriginalEntityState(customer);

            Response.Write(string.Format("Name:{0},Country:{1}<br/>", customer.ContactName, customer.Country));

           上面的代码执行效果如下:

    Name:zhuye,Country:Shanghai
    Name:Thomas Hardy,Country:UK

    批量操作

           下面的代码会导致提交N次DELETE操作:

            var query = from c in ctx.Customers select c;

            ctx.Customers.RemoveAll(query);

            ctx.SubmitChanges();

           应该使用sql语句进行批操作:

            string sql = String.Format("delete from {0}", ctx.Mapping.GetTable(typeof(Customer)).TableName);

            ctx.ExecuteCommand(sql);

           对于批量更新操作也是同样道理。

    一步一步学Linq to sql(九):其它补充(三)
    2010年05月17日 星期一 17:04

    撤销提交

            var customer = ctx.Customers.Single(c => c.CustomerID == "AROUT");

            customer.ContactName = "zhuye";

            customer.Country = "Shanghai";

            Response.Write(string.Format("Name:{0},Country:{1}<br/>", customer.ContactName, customer.Country));

            customer = ctx.Customers.GetOriginalEntityState(customer);

            Response.Write(string.Format("Name:{0},Country:{1}<br/>", customer.ContactName, customer.Country));

           上面的代码执行效果如下:

    Name:zhuye,Country:Shanghai
    Name:Thomas Hardy,Country:UK

    批量操作

           下面的代码会导致提交NDELETE操作:

            var query = from c in ctx.Customers select c;

            ctx.Customers.RemoveAll(query);

            ctx.SubmitChanges();

           应该使用sql语句进行批操作:

            string sql = String.Format("delete from {0}", ctx.Mapping.GetTable(typeof(Customer)).TableName);

            ctx.ExecuteCommand(sql);

           对于批量更新操作也是同样道理。

  • 相关阅读:
    ASP.NET中Cookie编程的基础知识
    一道编程题
    软件开发一点心得
    迅雷产品经理笔试题
    常用JS 1
    设计模式
    整理思路
    抽象工厂模式 Abstract Factory
    单件模式(Single Pattern)
    序列化
  • 原文地址:https://www.cnblogs.com/kevin2013/p/1749028.html
Copyright © 2020-2023  润新知