zoukankan      html  css  js  c++  java
  • 转:SubSonic 各种语句集合

    int recordsAffected = new Update(Northwind.Product.Schema)
    .Set(“UnitPrice”).EqualTo(100)
    .Where(“productid”).IsEqualTo(1).Execute();
    

    int records = new Update(Northwind.Product.Schema)
        .SetExpression(“UnitPrice”).EqualTo(“UnitPrice * 3″)
        .Where(“productid”).IsEqualTo(1)
        .Execute();

    Northwind.ProductCollection products =
        Northwind.DB.Select().From(“Products”)
        .Where(“categoryID”).IsEqualTo(5)
        .And(“productid”).IsGreaterThan(50)
        .ExecuteAsCollection<Northwind.ProductCollection>();

    Northwind.CustomerCollection customersByCategory = new Select()
        .From(Northwind.Customer.Schema)
        .InnerJoin(Northwind.Order.Schema)
        .InnerJoin(Northwind.OrderDetail.OrderIDColumn, Northwind.Order.OrderIDColumn)
        .InnerJoin(Northwind.Product.ProductIDColumn, Northwind.OrderDetail.ProductIDColumn)
        .Where(“CategoryID”).IsEqualTo(5)
        .ExecuteAsCollection<Northwind.CustomerCollection>();

    class TestProduct {
    private int _id;
    public int ProductID {
    get { return _id; }
    set { _id = value; }
    }
    private string _name;
    public string ProductName {
    get { return _name; }
    set { _name = value; }
    }
    private decimal _price;
    public decimal UnitPrice {
    get { return _price; }
    set { _price = value; }
    }
    }

    可以这样查:

    List<TestProduct> result = new
    Select(“productid”, “productname”, “unitprice”)
    .From(Northwind.Product.Schema)
    .ExecuteTypedList<TestProduct>();

    Constraint Expressions
    If you have some complicated expressions, you can add those in too:

    Northwind.ProductCollection products = new Select(Northwind.Product.Schema)
    .WhereExpression(“categoryID”).IsEqualTo(5).And(“productid”).IsGreaterThan(10)
    .OrExpression(“categoryID”).IsEqualTo(2).And(“productID”).IsBetweenAnd(2, 5)
    .ExecuteAsCollection<Northwind.ProductCollection>();
    double result = new
        Select(Aggregate.Sum(“UnitPrice*Quantity”, “ProductSales”))
        .From(Northwind.OrderDetail.Schema)
        .ExecuteScalar<double>();

    int records = new Select().From(Northwind.Product.Schema)
        .Where(“productid”).In(1, 2, 3, 4, 5)
        .GetRecordCount();

    int records = new Select(Northwind.Product.Schema)
        .Where(“productid”)
        .In(
            new Select(“productid”).From(Northwind.Product.Schema)
            .Where(“categoryid”).IsEqualTo(5)
            )
        .GetRecordCount();

    SubSonic.SqlQuery sel = new Select(
    new Aggregate("统计","countY", AggregateFunction.Sum),
    new Aggregate("年", "年", AggregateFunction.GroupBy))
    .From("表");

    Doing It All At Once With InlineQuery
    One thing we didn’t have, necessarily, is a “back door” in SubSonic so you could let yourself out after coding yourself into a corner :). This can happen with any ORM tool and it’s one major reason I’ve embraced Views and Stored Procs for the more complex stuff. But sometimes (if you’re Jeff Atwood) you might not want to deal with our API - just get your data.

    For you, I made InlineQuery. It will execute your query happily, and parameterize it too so you don’t get yourself SQL-injected:

    Northwind.ProductCollection products=
       new InlineQuery()
        .ExecuteAsCollection<Northwind.ProductCollection>
        (“SELECT productID from products WHERE productid=@productid”, 1);

    SubSonic will see “@productid” and build a parameterized statement for it on the fly, then execute the results into a collection for you. Embedding SQL is almost never optimal - but if you need it, we got ya covered.

    Insert uses params and nested Select() statements as well:

    int recordsAffected = new Insert().Into(Northwind.Category.Schema)
        .Values(“Test”, “TestDescription”, DBNull.Value)
        .Execute();
    int recordsAffected = new Insert().Into(Northwind.Category.Schema)
        .Select(new Select(“CategoryName”, “Description”, “Picture”).From(Northwind.Category.Schema))
        .Execute();

    You may have noticed that you can use one of three things in a Select() constructor - a column list, a table schema, or nothing at all. If you pass in the schema you can save yourself from typing “From(…)” but you lose a little readability. If you pass in nothing then you’ll need to pass in a from - either way, with the latter two, a fully qualified column list will be built for you.

    Updates are pretty simple as well:

    int recordsAffected = new Update(Northwind.Product.Schema)
        .Set(“UnitPrice”).EqualTo(100)
        .Where(“productid”).IsEqualTo(1).Execute();
    

    We’ve been asked a few times to support SQL Expressions here - in other words allowing for “UPDATE table set column=column+1″, and you can now do that using SetExpression:

    int records = new Update(Northwind.Product.Schema)
        .SetExpression(“UnitPrice”).EqualTo(“UnitPrice * 3″)
        .Where(“productid”).IsEqualTo(1)
        .Execute();

    Deletes work much the same way - and hopefully you’re seeing a pattern here:

    int recordsAffected = Northwind.DB.Delete()
        .From(Northwind.Region.Schema)
        .Where(“regiondescription”).Like(“test%”)
        .Execute();
     
    .AndExpression("ProductID").IsEqualTo(10) 
    .Or("ProductID").IsEqualTo(20)
    .Or("UnitPrice").IsEqualTo(20)
    .CloseExpression()


    简单介绍说明以下如果要使用带括号表达式必须使用AndExpression或者OrExpression
    使用AndExpression的后,其实就是在And的后先加入个(然后直到CloseExpression思路方法出现后才会加上)
  • 相关阅读:
    nopcommerce商城系统--源代码结构和架构
    nopcommerce商城系统--如何编写一个插件
    ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
    【js与jquery】电子邮箱、手机号、邮政编码的正则验证
    也用 Log4Net 之走进Log4Net (四)
    也用 Log4Net 之将自定义属性记录到文件中 (三)
    也用 Log4Net 之将日志记录到数据库的后台实现 (二)
    也用 Log4Net 之将日志记录到数据库的配置 (一)
    log4net 将日志写入数据库
    ASP.NET MVC 3 入门级常用设置、技巧和报错
  • 原文地址:https://www.cnblogs.com/davinci/p/1652389.html
Copyright © 2011-2022 走看看