为什么这样可以实现取cache中的值。
if (System.Web.HttpContext.Current.Cache.Get("DsFLink") == null)
{
using(SqlConnection cn = new SqlConnection(Global.ConnectionString))
{
string Sql = @"SELECT top "+TopNum+" Domain,SiteName FROM dbo.un_tbl_SiteConfig WHERE Domain <>  N'"+_Domain+@"' and flag=1 ORDER BY NEWID()";
try
{
DataSet ds = SqlHelper.ExecuteDataset(cn,CommandType.Text,Sql);
        System.Web.HttpContext.Current.Cache.Insert("DsFLink",ds,null,DateTime.Now.AddMinutes(1),TimeSpan.Zero);
return ds;
}
catch(Exception ex)
{
cn.Close();
throw new Exception(ex.Message);
}
}
}
else
{
return (DataSet)System.Web.HttpContext.Current.Cache.Get("DsFLink");
}

解决方案 »

  1.   

    而,这样不可以呢
    object obj = UnCache.getCache("DsFLink");
    if (obj == null)
    {
    using(SqlConnection cn = new SqlConnection(Global.ConnectionString))
    {
    string Sql = @"SELECT top "+TopNum+" Domain,SiteName FROM dbo.un_tbl_SiteConfig WHERE Domain <>  N'"+_Domain+@"' and flag=1 ORDER BY NEWID()";
    try
    {
    DataSet ds = SqlHelper.ExecuteDataset(cn,CommandType.Text,Sql);
    UnCache.setCache("DsFlink",ds);
    return ds;
    }
    catch(Exception ex)
    {
    cn.Close();
    throw new Exception(ex.Message);
    }
    }
    }
    else
    {
    return (DataSet)obj;
    }getCache是放在另外一个类中。这样的结果是cache永远为空,就是不执行return (DataSet)obj;
    public static object getCache(string strVal)
    {
    try
    {
    return System.Web.HttpContext.Current.Cache.Get(strVal);//[strVal];
    }
    catch
    {
    return null;
    }
    }public static void setCache(string strVal,object obj)
    {
    HttpContext.Current.Cache.Insert(strVal,obj,null,DateTime.Now.AddMinutes(1),TimeSpan.Zero);
    }
      

  2.   

    靠,高人都到那去了。是不是大家懒得看我的代码。就是把dataset缓存到cache中。
      

  3.   

    这样都可以,把注释掉的取消,再注释掉它下面的一行,就不行了。怎么这么怪呢object obj = UnCache.getCache("DsFLink");
    if (obj == null)
    {
    using(SqlConnection cn = new SqlConnection(Global.ConnectionString))
    {
    string Sql = @"SELECT top "+TopNum+" Domain,SiteName FROM dbo.un_tbl_SiteConfig WHERE Domain <>  N'"+_Domain+@"' and flag=1 ORDER BY NEWID()";
    try
    {
    DataSet ds = SqlHelper.ExecuteDataset(cn,CommandType.Text,Sql);
    //UnCache.setCache("DsFlink",ds);
    System.Web.HttpContext.Current.Cache.Insert("DsFLink",ds,null,DateTime.Now.AddMinutes(1),TimeSpan.Zero);
    return ds;
    }
    catch(Exception ex)
    {
    cn.Close();
    throw new Exception(ex.Message);
    }
    }
    }
    else
    {
    return (DataSet)obj;
    }
      

  4.   

    默认的时间在webconfig里。
      

  5.   

    UnCache.setCache("DsFlink",ds);
    你直接改为
    Cache("DsFlink") =ds
    看下
      

  6.   

    nyf1220(我是党员----不过听说最近风声紧,打算换名字) 怎么设置,一般设置多长时间呢?
      

  7.   

    UnCache.setCache("DsFlink",ds);object obj = UnCache.getCache("DsFLink");终于找到了,是DsFlink和DsFLink中的l大小写错误了。