赞
踩
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;
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。