中继器和缓存数据集的奇怪问题 - 仅显示最后一行

发布于 2024-12-27 01:36:35 字数 1256 浏览 0 评论 0原文

我有一段相对简单的代码,如下所示:

        Dim oShow As DataSet = Nothing
        Dim cacheKey As String = String.Format("AsyncCacheFor_agenda_{0}", ShowID)

        If Not IsNothing(Cache(cacheKey)) Then
            oShow = DirectCast(Cache(cacheKey), DataSet)
        Else
            oShow = DataServers.dsTMW.GetAgenda(ShowID, 0, "", 0, True)
            Cache.Insert(cacheKey, oShow, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, New TimeSpan(1, 0, 0))
        End If
        phSearch.Visible = True


        oShowRow = oShow.Tables(0).Rows(0)


        oTracks = oShow.Tables(1)
        oSearchResults = oShow.Tables(5)


        If Not IsNothing(oSearchResults) AndAlso oSearchResults.Rows.Count > 0 Then
            rptSearch.Visible = True
            phNoResults.Visible = False
            rptSearch.DataSource = oSearchResults
            rptSearch.DataBind()
        Else
            rptSearch.Visible = False
            phNoResults.Visible = True

        End If

“rptSearch”是一个Repeater,而GetAgenda()方法通过存储过程从数据库读取数据集。该数据集有 6 个不同的表。从数据库读取数据集时,Repeater 显示 36 行,这是预期的结果。从缓存中读取时,它仅显示一行,即最后一行。奇怪的是,当我调试时,我在两种情况下都看到oSearchResults.Rows.Count = 36

有谁知道为什么会发生这种情况?

I have a relatively simple piece of code as follows:

        Dim oShow As DataSet = Nothing
        Dim cacheKey As String = String.Format("AsyncCacheFor_agenda_{0}", ShowID)

        If Not IsNothing(Cache(cacheKey)) Then
            oShow = DirectCast(Cache(cacheKey), DataSet)
        Else
            oShow = DataServers.dsTMW.GetAgenda(ShowID, 0, "", 0, True)
            Cache.Insert(cacheKey, oShow, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, New TimeSpan(1, 0, 0))
        End If
        phSearch.Visible = True


        oShowRow = oShow.Tables(0).Rows(0)


        oTracks = oShow.Tables(1)
        oSearchResults = oShow.Tables(5)


        If Not IsNothing(oSearchResults) AndAlso oSearchResults.Rows.Count > 0 Then
            rptSearch.Visible = True
            phNoResults.Visible = False
            rptSearch.DataSource = oSearchResults
            rptSearch.DataBind()
        Else
            rptSearch.Visible = False
            phNoResults.Visible = True

        End If

"rptSearch" is a Repeater and the GetAgenda() method reads a dataset from the database via stored procedure. This dataset has 6 different tables. When the dataset is read from the database, the Repeater displays 36 rows, the expected result. When it's read from the cache it only displays one row, the last one. What's strange is that when I debug, I see that oSearchResults.Rows.Count = 36 in both cases.

Does anyone have any idea why this might happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市 2025-01-03 01:36:35

要真正了解它是否与缓存有关,请尝试始终使用缓存中的数据集。在检查它是否在缓存中时,不要获取数据集然后使用它,而是获取它,将其放入缓存中,然后使用它。

因此,不要这样做:

If Not IsNothing(Cache(cacheKey)) Then
    oShow = DirectCast(Cache(cacheKey), DataSet)
Else
    oShow = DataServers.dsTMW.GetAgenda(ShowID, 0, "", 0, True)
    Cache.Insert(cacheKey, oShow, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, New TimeSpan(1, 0, 0))
End If

这样做:

If IsNothing(Cache(cacheKey)) Then
    oShow = DataServers.dsTMW.GetAgenda(ShowID, 0, "", 0, True)
    Cache.Insert(cacheKey, oShow, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, New TimeSpan(1, 0, 0))
End If

oShow = DirectCast(Cache(cacheKey), DataSet)

这不会“解决”您的问题,但会缩小问题范围。如果你总是得到 1 行,那么你就知道这与将数据集从缓存中拉出有关。如果我不得不猜测,我会说在您的代码片段之外还发生了其他事情。

To truly see if it has anything to do with the cache, try always using the Dataset from the cache. On the check to see if it is in the cache, rather than going and getting the Dataset and then using it, go get it, put it in the cache, then use it.

So, rather than this:

If Not IsNothing(Cache(cacheKey)) Then
    oShow = DirectCast(Cache(cacheKey), DataSet)
Else
    oShow = DataServers.dsTMW.GetAgenda(ShowID, 0, "", 0, True)
    Cache.Insert(cacheKey, oShow, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, New TimeSpan(1, 0, 0))
End If

Do this:

If IsNothing(Cache(cacheKey)) Then
    oShow = DataServers.dsTMW.GetAgenda(ShowID, 0, "", 0, True)
    Cache.Insert(cacheKey, oShow, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, New TimeSpan(1, 0, 0))
End If

oShow = DirectCast(Cache(cacheKey), DataSet)

This won't "fix" your issue, but it will narrow down the isue. If you get 1 row always, then you know it has to do with pulling the Dataset out of the cache. If I had to guess though, I'd say that there is something else going on outside of your code snippet.

寒尘 2025-01-03 01:36:35

我发现了问题所在。我在 ItemDataBound 事件中执行此操作:

Dim dvRows As DataView = oSearchResults.DefaultView
dvRows.RowFilter = String.Format("Submission_id={0} AND speaker_id Is Not Null", oRow("Submission_id"))

一旦我将其更改为:

Dim dvRows As DataView = oSearchResults.Copy().DefaultView

问题就消失了。我想知道为什么这只是缓存时的问题。

I figured out the problem. I was doing this in the ItemDataBound event:

Dim dvRows As DataView = oSearchResults.DefaultView
dvRows.RowFilter = String.Format("Submission_id={0} AND speaker_id Is Not Null", oRow("Submission_id"))

Once I changed that to:

Dim dvRows As DataView = oSearchResults.Copy().DefaultView

The problem went away. I wonder why this was only a problem when it was being cached.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文