【问题标题】:Show timestamp and not date in chart - SQL在图表中显示时间戳而不是日期 - SQL
【发布时间】:2019-01-18 17:08:42
【问题描述】:

我正在为客户制作数据处理页面。

我制作了一个图表来显示我的 SQL 服务器中的数据。
在我的 x 轴上,我有一个日期,但整个列还包含一个时间戳,例如“08/01/2019 14:34”,但不可见。

如下面的链接图片所示,如果可能,如何格式化 x 轴值以仅显示时间或 dbo 中的整个值?

我是否必须在屏幕上格式化图表的大小,如果是,我该怎么做?我尝试在图表设置中寻找一个选项。

这是代码的sn-p:

Private Sub dgvSiteChart() 'Load data from dbo
    DateandTimeString24H = "'" & Date.Now.Year & "-" & Date.Now.Month & "-" & Date.Now.Day & " 00:00:00.000'" & " And " & "'" & Date.Now.Year & "-" & Date.Now.Month & "-" & Date.Now.Day & " 23:59:59.000'"
    SQLCon = New SqlConnection
    SQLCon.ConnectionString = "Data Source=" & System.Net.Dns.GetHostName() & "\KVMSQL;Initial Catalog=MHA;User ID=#####;Password =######"
    Dim READER As SqlDataReader

    Try
        SQLCon.Open()
        Dim Query As String
        Query = "SELECT DateAndTime,Tagindex,Val FROM dbo.FT WHERE tagindex=0 and DateAndTime between " & DateandTimeString24H & "ORDER BY DateAndTime"
        SQLCmd = New SqlCommand(Query, SQLCon)
        READER = SQLCmd.ExecuteReader
        While READER.Read
            chrtReportMchn.Series("LOG").Points.AddXY(READER("DateAndTime"), READER("Val"))
        End While
        SQLCon.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        SQLCon.Dispose()
    End Try
End Sub

日期和时间格式为dd/MM/yyyy HH:mm(24H)

更新:10/1-19

在@Markus 和他的评论的帮助下,我已经想通了。 代码是用FORMAT(CAST(DateAndTime as time), 'hh\:mm\ ')编辑的,放在DateAndTime前面。

Try
    SQLCon.Open()
    Dim Query As String
    Query = "SELECT FORMAT(CAST(DateAndTime as time), 'hh\:mm\ ') DateAndTime,Tagindex,Val FROM dbo.FT WHERE tagindex=0 and DateAndTime between " & DateandTimeString24H & "ORDER BY DateAndTime"
    SQLCmd = New SqlCommand(Query, SQLCon)
    READER = SQLCmd.ExecuteReader
    While READER.Read

【问题讨论】:

  • 当你只想显示时间时,为什么不在选择查询中强制转换日期时间列
  • 像这样 FORMAT(CAST(DateAndTime as time), N'hh\:mm\:ss') [time]
  • 嗨,马库斯。谢谢回复。我对此很陌生,所以我似乎无法确定应该将 FORMAT(CAST(DateAndTime as time), N'hh\:mm\:ss') [time] 行放在我的代码中的什么位置。我在 Query 中尝试过,但它以空白图表响应,当我在 SQL Server mngt studio 中尝试时出现错误
  • 我想通了。见帖子更新。谢谢马库斯。

标签: vb.net charts


【解决方案1】:

您需要Dispose 所有与数据库相关的对象(连接、命令、读取器)。最好使用Using,而不是在Finally 中手动操作。
您必须使用参数而不是string concatenation。这样您也不会失去一天的最后一秒(23:59:59 到次日 00:00:00 之间)。
您可以在连接字符串中使用一个点来表示本地计算机。
如果您不使用它,则无需返回TagIndex
您可以在客户端格式化日期。

Private Sub dgvSiteChart()
    Try
        Using SQLCon = New SqlConnection("Data Source=.\KVMSQL;Initial Catalog=MHA;User ID=#####;Password =######")
            SQLCon.Open()
            Using SQLCmd = New SqlCommand("SELECT DateAndTime,Val FROM dbo.FT WHERE tagindex=0 and DateAndTime >= @from_inclusive and DateAndTime < @to_exclusive ORDER BY DateAndTime", SQLCon)
                Dim captured_date = DateTime.Today ' Because it may change if calling this around midnight

                SQLCmd.Parameters.Add("@from_inclusive", SqlDbType.DateTime).Value = captured_date
                SQLCmd.Parameters.Add("@to_exclusive", SqlDbType.DateTime).Value = captured_date.AddDays(1)

                Using READER = SQLCmd.ExecuteReader()
                    While READER.Read()
                        chrtReportMchn.Series("LOG").Points.AddXY(CType(READER("DateAndTime"), Date).ToShortTimeString(), READER("Val"))
                    End While
                End Using
            End Using
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多