asp.net 缓存DataTable对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OrderTack.BLL;

public partial class _Default : System.Web.UI.Page
{
    OrderTrackRouterBLL bll = new OrderTrackRouterBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindGrid();
        }
    }

    /// 
    /// 获取用户信息
    /// 
    DataTable BindUserMeg(string name)
    {
        DataTable dtUser = bll.GetUserMsg(name);
        return dtUser;
    }


    void bindGrid()
    { //查询记录缓存 DataTable
        DataTable dt = BindUserMeg("");
        Cache.Insert("DSN", dt, null, DateTime.Now.AddMinutes(2), System.Web.Caching.Cache.NoSlidingExpiration);

        GridView1.DataSource = Cache["DSN"];
        GridView1.DataBind();
    }


    protected void Button1_Click(object sender, EventArgs e)
    {//linq模糊查询

       DataTable dtA = (DataTable)Cache["DSN"];

        var query = (from a in dtA.AsEnumerable()
                    let username = a.Field("OrderTrackerUserName")
                    where username.Contains(TextBox1.Text)
                    select new {
                                    OrderTrackerID=a.Field("OrderTrackerID"),
                                    OrderTrackerUserName =username,
                                    OrderTrackerRealName = a.Field("OrderTrackerRealName")
                               });

        GridView1.DataSource = query;
        GridView1.DataBind();
    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>





    
    
     
    
    

    


    

一般处理程序

<%@ WebHandler Language="C#" Class="SearchUserName2" %>

using System;
using System.Web;

using OrderTack.BLL;
using System.Data;
using System.Text;



public class SearchUserName2 : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        
        string strName = context.Request.QueryString["q"];


        StringBuilder sb = new StringBuilder();


        DataTable dtA = (DataTable)HttpContext.Current.Cache["DSN"];


        var query = (from a in dtA.AsEnumerable()
                     let username = a.Field("OrderTrackerUserName")
                     where username.Contains(strName)
                     select new
                     {
                         OrderTrackerID = a.Field("OrderTrackerID"),
                         OrderTrackerUserName = username,
                         OrderTrackerRealName = a.Field("OrderTrackerRealName")
                     });

        foreach (var item in query)
        {
            sb.Append("[" + item.OrderTrackerID.ToString() + "]" + item.OrderTrackerUserName).Append("\n");
        }
        
        
        context.Response.Write(sb.ToString());
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}



你可能感兴趣的:(asp.net)