【问题标题】:How to retrieve datetime (datetype only) from access 2007 database using vb.net 2010?如何使用 vb.net 2010 从 access 2007 数据库中检索日期时间(仅限日期类型)?
【发布时间】:2014-09-28 20:56:27
【问题描述】:

我的要求是让用户从 DateTimePicker 中选择一个日期,然后 GataGridView 将检索并显示与 DateTimePicker 中的值匹配的信息。

 Private Sub DateTimePicker3_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker3.ValueChanged
    dailyloadglog()
End Sub

.

Public Sub dailyloadglog()
    Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=True;")
    Try
        Dim OOL As String = "SELECT Glogtbl.DIN, Glogtbl.clock, Int([clock]) AS JustDate, [clock]-Int([clock]) AS JustTime FROM Glogtbl WHERE JustDate = " & DateTimePicker3.Value.ToShortDateString & ""
        Dim cmd As New OleDbCommand
        Dim odpt As New OleDbDataAdapter
        Dim tbl As New DataTable
        With cmd
            .CommandText = OOL
            .Connection = connection
        End With
        With odpt
            .SelectCommand = cmd
            .Fill(tbl)
        End With
        DataGridView4.Rows.Clear()
        For i = 0 To tbl.Rows.Count - 1
            With DataGridView4
                .Rows.Add(tbl.Rows(i)("DIN"), tbl.Rows(i)("clock"), tbl.Rows(i)("JustDate"), tbl.Rows(i)("JustTime"))
            End With
        Next
        connection.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

ex.exception 给了我这个:

当我尝试检索所有日期/时间列并显示结果时:

 Dim OOL As String = "SELECT Glogtbl.DIN, Glogtbl.clock, Int([clock]) AS JustDate, [clock]-Int([clock]) AS JustTime FROM Glogtbl"

JustDate 值仍保留格式长日期时间值。因此,与 datetimepicker.value 进行比较的值总是错误的。最终什么都没有检索到!!!

谁能指导我这个?!

【问题讨论】:

    标签: vb.net datetime


    【解决方案1】:

    不要使用字符串连接将值插入 SQL 代码。始终使用参数。这样做可以避免您遇到的分隔符或格式不正确等问题。例如

    myCommand.CommandText = "SELECT * FROM MyTable WHERE MyColumn = @MyColumn"
    myCommand.Parameters.AddWithValue("@MyColumn", myValue)
    

    您可以在很多地方找到有关 ADO.NET 参数的更多信息,例如

    http://jmcilhinney.blogspot.com.au/2009/08/using-parameters-in-adonet.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      相关资源
      最近更新 更多