zoukankan      html  css  js  c++  java
  • 9,高级技巧补充

    概要:

           外部映射,处理空值,已编译查询,获取信息,撤销提交,批量操作

      此系列是我的学习笔记,仅供个人复习,原链接在第一篇中有。

    内容:

           外部映射文件

     

           我们可以使用sqlmetal命令行工具来生成外部映射 文件,使用方法如下:

    1、开始菜单 -》 VS2008 -》VS工具 -》VS2008命令行提示

    2、输入命令:

    D:\Program Files\Microsoft Visual Studio 9.0\VC>sqlmetal /conn:server=xxx;

    database=Northwind;uid=xxx;pwd=xxx /map:c:\northwind.map /code:c:\northwind.cs

    3、这样,我们就可以在C盘下得到一个xml映射文件和C#的实体类代码

    4、把.cs文件添加到项目中来(放到App_Code目录),然后使用下面的代码加载映射文件:

    String path = @"C:\Northwind.map";

    XmlMappingSource xms = XmlMappingSource.FromXml(File.ReadAllText(path));

    Northwind ctx = new Northwind("server=xxx;database=Northwind;uid=xxx;pwd=xxx", xms);

    5、现在就可以照常进行其它工作了。使用sqlmetal可以很方便的同步数据库与实体和映射文件。每次修改数据库结构,从dbml设计器上删除表、存储过程然后再重新添加也是很麻烦的事情。

    处理空值

     

              var count = (from c in ctx.Customers where c.Region == null select c).Count();

            Response.Write(count + "<br/>");

            var query = from emp in ctx.Employees select emp.ReportsTo;

            foreach (Nullable<int> r in query)

            {

                Response.Write(r.HasValue ? r.Value.ToString() + "<br/>" : "没有<br/>");

            }

    已编译查询

     

           对于一些在项目中经常 被用到的查询可以封装成已编译查询,这样就能提高执行效率:

    static class Queries

    {

        public static Func<NorthwindDataContext, string, IQueryable<Customer>>

            CustomersByCity = CompiledQuery.Compile((NorthwindDataContext ctx, string city) => from c in ctx.Customers where c.City == city select c);

    }

           调用查询方式如下:   

            GridView1.DataSource = Queries.CustomersByCity(ctx, "London");

            GridView1.DataBind();

    获取一些信息

     

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

            Response.Write("Provider类型:" + ctx.Mapping.ProviderType + "<br/>");

            Response.Write("数据库:" + ctx.Mapping.DatabaseName + "<br/>");

            Response.Write("表:" + ctx.Mapping.GetTable(typeof(Customer)).TableName + "<br/>");

            Response.Write("表达式:" + query.Expression.ToString() + "<br/>");

            Response.Write("sql:" + query.Provider.ToString() + "<br/>");

    撤销提交

     

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

  • 相关阅读:
    Redis高可用配置(Keepalived)
    Redis断线重连编码注意事项
    Redis持久化配置
    真正实现Netty私有协议开发
    winform 程序隐藏窗口运行
    VirtualBox设置共享文件夹和镜像访问的方法
    SSIS父子维度
    SSIS 抽取excel出错:所请求的 OLE DB 访问接口 Microsoft.ACE.OLEDB.12.0 尚未注册
    .net C#实现 中文转Unicode、Unicode转中文 及与js对应关系
    HttpHelper使用记录
  • 原文地址:https://www.cnblogs.com/yaoge/p/1816736.html
Copyright © 2011-2022 走看看