有人可以告诉我为什么这不起作用?如果我将测试函数放入后面的代码中就可以了。如果我把它放到测试类中,数据总是没有。
答案 0 :(得分:1)
您必须返回一些DataTable值!
更新:我添加SqlDependency.Start(connstring)
但缺少此行,您应该看到错误。这表明测试不是什么
Public Class TestClass
Public Shared Function Test() As DataTable
Test = DirectCast(HttpContext.Current.Cache("Test"), DataTable)
If Test Is Nothing Then
Dim sql As String = "SELECT * from categories"
Dim constring As String = ConfigurationManager.ConnectionStrings("HalloweenConnectionString").ConnectionString
Using conn As New SqlConnection(constring)
Using cmd As New SqlCommand(sql, conn)
Dim dep As New SqlCacheDependency(cmd)
SqlDependency.Start(constring)
Test = New DataTable
conn.Open()
Test.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection))
HttpContext.Current.Cache.Insert("Test", Test, dep, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(24))
End Using
End Using
End If
End Function
结束班
答案 1 :(得分:0)
这就是我最终这样做的方式。我仍然不清楚为什么我必须通过sqlcachedepency。
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim dep As SqlCacheDependency = Nothing
ASPxGridView1.DataSource = TestClass.Test(dep)
ASPxGridView1.DataBind()
ASPxLabel1.Text = Now
End Sub
End Class
Public Class TestClass
Public Shared Function Test(ByRef dep As SqlCacheDependency) As DataTable
Test = DirectCast(HttpRuntime.Cache("Test"), DataTable)
If Test Is Nothing Then
Dim sql As String = "SELECT field1,field2,field3 FROM test "
Using conn As New SqlConnection(Materials.Conn)
Using cmd As New SqlCommand(sql, conn)
dep = New SqlCacheDependency(cmd)
Test = New DataTable
conn.Open()
Test.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection))
HttpRuntime.Cache.Insert("Test", Test, dep, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(24))
End Using
End Using
End If
End Function
End Class