为什么这样可以实现取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");
}
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");
}
解决方案 »
- 如何在模式窗口关闭时刷新原始页面的数据?
- 寻求异地相同结构的表,除关键字字段之外的字段内容不相同,想以其中的一个表为对照表,把2个表的内容整合到一个girdview上的方法
- 哪位能直线这种效果窗体效果~~
- ASP.NET的下载功能如何实现迅雷下载?
- 问两道简单unix的题目(不知道发哪个分论坛,就发这边吧)
- ajax.net初学问题
- javascript中<!--和//-->是什么意思
- 为什么like后不可加%,模糊搜索?
- 在ASP.NET中如何用vb.net动态创建数据库?
- 请问ASP.net部署完成之后为何不能运行ASPX文件?
- 各位大侠帮忙,发布网站的奇怪问题,google上死活搜不到解决方法!!!
- 关于datagrid ,在线等,很急,希望大家帮帮忙!!!在线等。。。。。
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);
}
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;
}
你直接改为
Cache("DsFlink") =ds
看下