非静态字段,方法或属性所需的对象引用

我想使用Caching.Cache(…)方法,如下所示:

Cache.Insert("Interview Questions", datatable, sqlcachedep) 

要么

 System.Web.Caching.Cache.Insert("Reading List", datatable, sqlcachedep); 

变量没有问题,但在任何一种情况下都会收到此错误消息:

错误1 – 非静态字段,方法或属性’System.Web.Caching.Cache.Insert(string,object,System.Web.Caching.CacheDependency)’需要对象引用

我怎样才能解决这个问题?

谢谢

这是正确的说法。 你应该尝试类似的东西:

 HttpContext.Current.Cache.Insert(...); 

Cache.Insert不是静态方法(静态方法在文档中的方法图标附近用“S”表示。)您需要一个实例来调用Insert方法。 HttpContext.Current.Cache返回与当前应用程序关联的Cache对象。

你需要这样做

 Page.Cache.Insert() 

(我假设你在谈论ASP.Net)。 您正在调用Cache作为类,而不是它的实例。

试试这个(从内存中):

 HttpApplication.Context.Cache.Insert("Reading List", datatable, sqlcachedep);