。net 之view筛选

ASP.NET 缓存与数据查询
本文介绍了一个使用 ASP.NET 的应用程序如何通过缓存机制提高数据查询效率的方法。具体展示了如何利用 Web.Config 文件作为缓存依赖,在缓存中存储 DataSet 对象并实现基于条件的数据过滤。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Caching;
using System.Data;

public partial class CACHE查询条件 : System.Web.UI.Page
{
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        string cond = "";
        if (this.DropDownList1.SelectedIndex > 0)
        {
             cond += "stu_sex='" + this.DropDownList1.SelectedValue + "'";
          
        }
        else
        {
             cond += "1=1";   
        }

        if (this.TextBox1.Text != "")
        {
            cond += " and age=" + this.TextBox1.Text;
        }
        ViewState["cond"] = cond;
        Page_Load(null, null);
    }
    //页面加载时,将DataSet插入到委托中去。
    protected void Page_Load(object sender, EventArgs e)
    {

        //首先从Cache中获取Dataset
        DataSet students = (DataSet)Cache["Students"];
        //如果Cache中不存在DataSet,则从数据库中获取DataSet,并添加到缓存中
        if (students == null)
        {
            students = GetStudentDataSet();


      
            //创建一个基于web.config配置文件的依赖,当该文件发生变化时,则从缓存中移除缓存项。
            CacheDependency cd = new CacheDependency(Server.MapPath("web.config"));

            //在Insert中指定该依赖项,并且指定缓存的优先级为高优先级,传递一个传托。
            Cache.Insert("Students", students, cd, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromHours(1));
        }

        DataView view = students.Tables[0].DefaultView;
        if (ViewState["cond"] != null)
        {
            view.RowFilter = ViewState["cond"].ToString();
        }
        GridView1.DataSource = view;
        //GridView1.DataSource = students;

        GridView1.DataBind();
    }
    /// <summary>
    /// 从数据库中获取产品信息,并返回一个DataSet对象
    /// </summary>
    /// <returns></returns>
    protected DataSet GetStudentDataSet()
    {
        string sql = "select * from t_student";
        DataSet ds = DBHelper.SqlHelper.ExecuteDataSetText(sql, null);
        return ds;
    }
}

  

转载于:https://www.cnblogs.com/mengluo/p/6078295.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值