当前位置:   article > 正文

sqlsugar对mysql效率高_高性能ORM 框架之 MySqlSugar (下)

sugarorm mysql isnullable

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using MySqlSugar;

using Models;

namespace WebTest.Demo

{

///

/// 过滤器

///

public partial class Filter : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

using (SqlSugarClient db = SugarDaoFilter.GetInstance())//开启数据库连接

{

//设置走哪个过滤器

db.CurrentFilterKey = "role";

//queryable

var list = db.Queryable().ToList(); //通过全局过滤器对需要权限验证的数据进行过滤

//相当于db.Queryable().Where("id=@id",new{id=1})

//sqlable

var list2 = db.Sqlable().From("s").SelectToList("*");

//同上

//sqlQuery

var list3 = db.SqlQuery("select * from Student where 1=1");

//同上

}

}

}

///

/// 扩展SqlSugarClient

///

public class SugarDaoFilter

{

//禁止实例化

private SugarDaoFilter(){

}

///

/// 页面所需要的过滤函数

///

private static Dictionary> _filterParas = new Dictionary>()

{

{ "role",()=>{

return new KeyValueObj(){ Key=" id=@id" , Value=new{ id=1}};

}

},

{ "org",()=>{

return new KeyValueObj(){ Key=" orgId=@orgId" , Value=new{ orgId=1}};

}

},

};

public static SqlSugarClient GetInstance()

{

string connection = System.Configuration.ConfigurationManager.ConnectionStrings[@"sqlConn"].ToString(); //这里可以动态根据cookies或session实现多库切换

var reval = new SqlSugarClient(connection);

reval.SetFilterFilterParas(_filterParas);

return reval;

}

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/135046
推荐阅读
相关标签
  

闽ICP备14008679号