【问题标题】:DIsplay real time SQL data on web form在 Web 表单上显示实时 SQL 数据
【发布时间】:2018-10-25 12:34:08
【问题描述】:

我正在做一个小项目,但目前卡在这个过程中,非常感谢您的帮助。

我正在尝试将我的一个 SQL 表中的数据显示到 Web 表单 (asp) 上,只要将数据输入到表中,该表单就会有效地更新。

我已经设法通过使用每 2 秒刷新一次页面的 META 标签来使其工作,但我知道这不是一种理想的方法。有人建议我在有新插入值时仅从服务器到客户端更新 Web 表单,但是我不知道如何处理。

请看下面我当前的代码。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Label1.Text = "Cache Refresh:" & _
        Date.Now.ToLongTimeString

    SqlDependency.Start(GetConnectionSTring())

    Using connection As New SqlConnection(GetConnectionSTring())
        Using Command As New SqlCommand(GetSQL(), connection)
            Dim Dependency As New SqlCacheDependency(Command)

            Dim NumberOfMinutes As Integer = 3
            Dim Expires As Date = _
                DateTime.Now.AddMinutes(NumberOfMinutes)

            Response.Cache.SetExpires(Expires)
            Response.Cache.SetCacheability(HttpCacheability.Public)
            Response.Cache.SetValidUntilExpires(True)

            Response.AddCacheDependency(Dependency)

            connection.Open()

            GridView1.DataSource = Command.ExecuteReader()
            GridView1.DataBind()

        End Using
    End Using

End Sub

Private Function GetConnectionSTring() As String

    Return "Data Source=xxxxxxxxx; Initial Catalog=Test; User ID=xxx; Password= xxx;"

End Function

Private Function GetSQL() As String

    Return "SELECT ProductCode, ProductName, Cost FROM dbo.OrderTempTable"

End Function

感谢您的意见。

【问题讨论】:

  • 您可能想搜索以下关键字:ajax、websocket、updatepanel。这些都是不同的方法。
  • 如果您想从服务器更新到客户端(即当服务器触发消息时),那么您必须使用 websockets(例如,.NET SignalR 是常用的工具)。其他方法(元刷新、ajax)是客户端到服务器的,因此您只需随机轮询,直到它发生更新。

标签: html asp.net vb.net


【解决方案1】:

我认为您必须每隔一段时间检查一次数据库。

THIS 适用于 PHP,但思路相同。

HERE 另一个例子。

【讨论】:

  • 请不要发布仅链接的答案,它只会被标记为低质量并被删除。相反,在您自己的答案中包含一些相关内容。链接可能会在一段时间后更改或消失。一定要提供一个链接作为答案的来源或指向更深入的讨论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 2019-09-14
  • 2013-03-18
  • 2021-11-18
相关资源
最近更新 更多