• 学习SubSonic的笔记 Version 2.1


    配置
    <section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" requirePermission="false"/>
     <connectionStrings>
            <add name="Northwind" connectionString="Data Source=.;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa;" />
        </connectionStrings>
        <SubSonicService defaultProvider="Northwind">
            <providers>
                <clear/>
                <add name="Northwind"
                     type="SubSonic.SqlDataProvider, SubSonic"
                     connectionStringName="Northwind"
                     generatedNamespace="Northwind"/>
            </providers>
        </SubSonicService>
     
    普通查询
     exec sp_executesql N'SELECT [dbo].[Products].[ProductID], [dbo].[Products].[ProductName], [dbo].[Products].[SupplierID], [dbo].[Products].[CategoryID], [dbo].[Products].[QuantityPerUnit],
    [dbo].[Products].[UnitPrice], [dbo].[Products].[UnitsInStock], [dbo].[Products].[UnitsOnOrder], [dbo].[Products].[ReorderLevel], [dbo].[Products].[Discontinued]
     FROM [dbo].[Products]
     WHERE [dbo].[Products].[CategoryID] = @CategoryID
    ',N'@CategoryID int',@CategoryID=1
     
     List<Product> products = new Select().From<Product>()
                        .Where(Product.CategoryIDColumn).IsEqualTo(1)
                        .ExecuteTypedList<Product>();
     
     exec sp_executesql N'SELECT [dbo].[Products].[ProductID], [dbo].[Products].[ProductName], [dbo].[Products].[SupplierID], [dbo].[Products].[CategoryID], [dbo].[Products].[QuantityPerUnit],
    [dbo].[Products].[UnitPrice], [dbo].[Products].[UnitsInStock], [dbo].[Products].[UnitsOnOrder], [dbo].[Products].[ReorderLevel], [dbo].[Products].[Discontinued]
     FROM [dbo].[Products]
     INNER JOIN [dbo].[Categories] ON [dbo].[Products].[CategoryID] = [dbo].[Categories].[CategoryID]
     WHERE ([dbo].[Products].[ProductID] > @ProductID0
     OR [dbo].[Products].[ProductID] < @ProductID
    )
     ORDER BY dbo.[Products].[ProductID] ASC
    ',N'@ProductID0 int,@ProductID int,@## varchar(8000)',@ProductID0=10,@ProductID=5,@##=NULL

     List<Product> products = new Select().From<Northwind.Product>()
        .InnerJoin(Northwind.Category.CategoryIDColumn,Northwind.Product.CategoryIDColumn)
        .WhereNorthwind.Product.Columns.ProductID).IsGreaterThan(10)
        .Or(Northwind.Product.ProductIDColumn).IsLessThan(5)
        .Close)
        .OrderAsc(Northwind.Product.ProductIDColumn)
        .ExecuteTypedList<Product>();
     
     --这个地方得注意
      .InnerJoin(Northwind.Category.CategoryIDColumn,Northwind.Product.CategoryIDColumn)
     -- 顺序不能写反了....要不然就出错了..哈哈
     
     --SubSonic源代码增加的方法
     .OrderAsc(Northwind.Product.ProductIDColumn)
     
     --Simple Paged Query(分页查询)
                SubSonic.SqlQuery q = Select.AllColumnsFrom<Product>().
                   Paged(1, 20).Where("productid").IsLessThan(100);
                  
     
     --分组查询方法
     SELECT CategoryID AS 'CategoryID', COUNT(1) AS 'CategoryIDs'
     FROM [dbo].[Products]
     GROUP BY CategoryID
     SqlQuery query = new Select(
        new Aggregate("CategoryID", "CategoryID", AggregateFunction.GroupBy),
        new Aggregate("1", "CategoryIDs", AggregateFunction.Count)
        ).From(Product.Schema);
    query.ExecuteDataSet();
    SELECT COUNT(1) AS 'CNTORDER', CONVERT(VARCHAR(7),ORDERDATE,120) AS 'ORDERDATE'
     FROM [dbo].[Orders]
     INNER JOIN [dbo].[Order Details] ON [dbo].[Orders].[OrderID] = [dbo].[Order Details].[OrderID]
     GROUP BY CONVERT(VARCHAR(7),ORDERDATE,120)
    new Select(new Aggregate("1", "CNTORDER", AggregateFunction.Count),
    new Aggregate("CONVERT(VARCHAR(7),ORDERDATE,120)", "ORDERDATE", AggregateFunction.GroupBy)).From(Northwind.Order.Schema)
    .InnerJoin(Northwind.OrderDetail.OrderIDColumn, Northwind.Order.OrderIDColumn)
    .ExecuteDataSet();
    SELECT COUNT([dbo].[Products].[ProductID]) AS 'CountX', [dbo].[Products].[CategoryID] AS 'CategoryID'
     FROM [dbo].[Products]
     GROUP BY [dbo].[Products].[CategoryID]
     ORDER BY CountX ASC
     List<Product> list = new Select(
        new Aggregate(Product.ProductIDColumn, SubEnumExtentions.CountX, AggregateFunction.Count),
        new Aggregate(Product.CategoryIDColumn, Product.CategoryIDColumn.ColumnName, AggregateFunction.GroupBy)
        ).From<Product>()
        .OrderAsc(SubEnumExtentions.CountX)
        .ExecuteTypedList<Product>();
                }
    自己新建了一个类
     
    public class SubEnumExtentions
        {
            public static string CountX = "CountX";
            public static string CountY = "CountY";
            public static string CountZ = "CountZ";
            public static string SumX = "SumX";
            public static string SumY = "SumY";
            public static string SumZ = "SumZ";
            public static string AvgX = "AvgX";
            public static string AvgY = "AvgY";
            public static string AvgZ = "AvgZ";
        }
    在SUBSONIC源代码增加了几个重载方法
    public SqlQuery OrderAsc(params TableSchema.TableColumn[] columns)等方法
    发现了里面N个BUG
    已修复了一些...
    1.如多表同名字段排序问题
    2.不存在一些TableSchema.TableColumn方法
    3.还发现一个问题未修改...
    List<Product> list = new Select().From<Product>()
        .WhereProduct.ProductIDColumn).IsGreaterThanOrEqualTo(4)
        .And(Product.ProductIDColumn).IsLessThan(10)
        .Close)
        .OrProduct.ProductIDColumn).IsGreaterThan(15)
        .And(Product.ProductIDColumn).IsLessThan(18)
        .Close)
        .OrderDesc(Product.ProductIDColumn)
        .ExecuteTypedList<Product>();
    竟然发现只传了3个参数进去...应该是4个参数的
    千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs
  • 相关阅读:
    函数指针与变长参数列表
    Finding intersection and union of two sets.
    依赖注入
    可达性分析算法
    java 虚拟机栈
    Java 堆
    java虚拟机>>程序计数器
    Java方法区(Method Area)
    Java 运行时常量池
    java引用
  • 原文地址:https://www.cnblogs.com/kingkoo/p/1692573.html
Copyright © 2020-2023  润新知