码迷,mamicode.com
首页 > 其他好文 > 详细

动态编译成存储过程

时间:2015-02-11 16:09:49      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

以下演示了将标准查询或SQL语句动态编译成存储过程

 对应的数据库帐号需要有创建存储过程的权限
以下查询会成成类似 ZautoSp_1E719B3EE5AFF6F4 的储储过程,删除后再查询时会自动生成

    var query = Code.ProductDataManage.Instance.GetLamadaQuery();
    //会按条件不同创建不同的存储过程
    query = query.Where(b => b.Id < 700);
    string name = Request["name"];
    if (!string.IsNullOrEmpty(name))
    {
        query = query.Where(b => b.InterFaceUser == name);
    }
    var list = Code.ProductDataManage.Instance.QueryList(query, compileSp: true);

    var helper = dbHelper;
    string sql = "select * from ProductData where datediff(d,addtime,@date)=0";
    helper.AddParam("date", date);
    return helper.AutoSpQuery(sql);

    var query = Code.ProductDataManage.Instance.GetLamadaQuery();
    query = query.Where(b => b.SupplierId == "1");
    int page = 1;
    int pageSize = 15;
    int count;
    query.Page(page, pageSize);
    //此查询会动态创建分页存储过程
    var list = Code.ProductDataManage.Instance.AutoSpPage(query, out count);

    //group分页测试
    int page = 1;
    int pageSize = 15;
    int count;
    var helper = dbHelper;
    var query = "ProductData with (nolock) group by ProductId";
    var fields = "ProductId,count(0) as total";
    var rowOver = "count(0) desc";
    var sort = "ProductId desc";
    /// 对GROUP进行分页
    /// 
    /// 
    /// 查询 如:ProductReceiptDetail with (nolock) group by styleid
    /// 查询字段 如:styleid,sum(num) as total
    /// 行排序 如:sum(num) desc
    /// 排序字段 如:total desc
    /// 每页大小
    /// 页索引
    /// 
    /// 
    return helper.AutoSpGroupPage(query, fields, rowOver, sort, pageSize, page, out count);
    

 

动态编译成存储过程

标签:

原文地址:http://www.cnblogs.com/hubro/p/4286329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!