0

I have a method, in a class, for retrieving some data from mssql database. I use this in many pages and web handlers depending on user's permission. I don't always want get data from database and need to use the advantage of caching. I am using vb.net framework 2 as my client and don't want to change it. I tried in many ways, but it didn't work. Can anyone help me...

My last try was this:

Dim myDataSet As New DataSet
myDataSet = CType(Web.HttpContext.Current.Cache.Get("myData"), DataSet)

If myDataSet Is Nothing Then
    myDataSet = GetData("select * from Table")
    Web.HttpContext.Current.Cache.Insert("myData", myDataSet, Nothing, DateTime.Now.AddSeconds(60), TimeSpan.Zero)
    'End If
End If

This is one of the methods from a class (testClass.vb), Any suggestions please...

4

1 回答 1

0

您应该在尝试将其分配给 DataSet 之前检查 Nothing 的缓存值,例如

Dim myDataSet As DataSet

If HttpContext.Current.Cache.Get("myData") Is Nothing

    myDataSet = GetData("select * from Table")
    HttpContext.Current.Cache.Insert("myData", myDataSet, Nothing, DateTime.Now.AddSeconds(60), TimeSpan.Zero)

Else

    myDataSet = HttpContext.Current.Cache.Get("myData") 

End If
于 2013-09-16T19:39:18.497 回答