以字符串变量作为缓存依赖,缓存数据

 protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void TextBoxIntelligent1_Focus(object sender, wzh.myWebControlsLib.TextBoxIntelligent.FocusEventArgs e)
        {
            string arg = e.tbText;
            if (Cache["dep"] == null)
            {
                Cache["dep"] = arg;
            }
            else if ((string)Cache["dep"] != arg)
            {
                Cache["dep"] = arg;
            }

            String[] dependencyKey = new String[1];
            dependencyKey[0] = "dep";
            CacheDependency dependency = new CacheDependency(null, dependencyKey);

            if (Cache["Data"] == null)
            {
                Cache.Insert("Data", getData(arg), dependency);
            }

            TextBoxIntelligent1.DataTable = (DataTable)Cache["Data"];

        }

        public DataTable getData(string arg)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["sqlConn"]);
            con.Open();
            SqlDataAdapter ad = new SqlDataAdapter("select [name] from tb_name where [name] like'" + arg + "%'", con);
            DataSet ds = new DataSet();
            ad.Fill(ds, "city");
            DataTable dt = ds.Tables["city"];
            con.Close();
            ad.Dispose();
            return dt;
        }

原文地址:https://www.cnblogs.com/zhuawang/p/2040396.html