我可以在asp.net中的webmethod中将数据表放在缓存中吗? [英] can i put datatable in cache in webmethod in asp.net?

查看:57
本文介绍了我可以在asp.net中的webmethod中将数据表放在缓存中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是将我的数据表保存在缓存中,这样每次我的webmethod从ajax自动完成扩展程序调用时,我的数据表都不会一次又一次地从数据库中填充。简而言之,我不想每次在文本框中按键时都要与数据库进行交互。可以这样做吗?此外,Ajax自动完成扩展程序工作死了为什么?我的代码如下:

what i want to do is to keep my datatable in cache so that each time my webmethod called from the ajax autocomplete extender my data table dosen't gets filled from database again and again. To be brief i dont want to interact with database everytime i press a key in textbox. can this be done? Also The Ajax autocomplete extender is working dead slow why? my code is as below:

[System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetCountries(string prefixText, int count)
    {
        DataTable dt = new DataTable();
        User objUser = new User();
        dt = objUser.GetDatatable();

        string Text = prefixText.ToUpper().ToString();
        List<string> LST = new List<string>();

         var query = from t in dt.AsEnumerable()
                     where t.Field<string>("DeptName").ToUpper().Contains(Text)
                     select new
                    {
                        DeptName = t.Field<string>("DeptName"),


                    };

         foreach (var i in query.Take(5))
         {
           LST.Add(i.DeptName.ToString().Trim());
         }

             return LST;
    }



在webmethod中进行缓存?谢谢。


an caching be done in webmethod?Thank you.

推荐答案

使用下面的代码....它会加快你的执行一段时间....



Use below code .... it will speed up your execution a while ....

[System.Web.Script.Services.ScriptMethod()]
       [System.Web.Services.WebMethod]
       public static List<string> GetCountries(string prefixText, int count)
       {
           DataTable dt = new DataTable();
           User objUser = new User();
           dt = objUser.GetDatatable();

           prefixText = prefixText.ToUpper();
           var query = (from DataRow r in dt.Rows
                       let DeptName = r.Field<string>("DeptName")
                        where DeptName.ToUpper().Contains(prefixText)
                       select DeptName).Take(5).ToList();
           return query;
       }





尝试在sql server中执行此逻辑,而不是尝试将结果保存在缓存中。 ..那将是最好的解决方案......:D



Try to execute this logic in sql server rather then trying to keep the result in cache... that will be the best solution ...... :D


这篇关于我可以在asp.net中的webmethod中将数据表放在缓存中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆