从2个日期和2个时间搜索查询

时间:2018-02-08 01:59:10

标签: ms-access-2010 vb.net-2010

我有4个Datetimepicker。 2 datetimepicker位于dateformat(datefrom,dateto)上,另外2个datetimepicker位于Timeformat(timefrom,timeto)上。 我想搜索一个只显示在datagridview中的表格,该表格只显示日期开始时间和时间,以及时间...但我的代码才能正常工作。

这是我的代码:

Dim fromto As String = String.Empty
    fromto &= "Select * from setplan where endplan >='" & dtimefrom.Text & "'and endplan >='" & dtimeto.Text & "' and [Dateinput] >='" & ddatefrom.Text & "' and [dateinput] <= '" & ddateto.Text & "'"

    Dim connection As String = ("provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Documents\planning.accdb;Persist Security Info=False;")
    Using conn As New OleDb.OleDbConnection(connection)
        Using cmd As New OleDb.OleDbCommand(fromto)
            With cmd
                .Connection = conn
                .CommandType = CommandType.Text
                .CommandText = fromto
            End With
            Try
                conn.Open()
                cmd.ExecuteNonQuery()
                Dim da As New OleDb.OleDbDataAdapter(cmd)
                Dim ds As New DataSet
                da.Fill(ds)

                If ds.Tables.Count > 0 Then
                    DataGridView1.DataSource = ds.Tables(0)
                End If



                conn.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try


        End Using
    End Using
End Sub

1 个答案:

答案 0 :(得分:0)

您需要为日期/时间值设置格式化的字符串表达式:

fromto &= "Select * from setplan where endplan >= #" & ddateto.Value.ToString("yyyy'/'MM'/'dd") & " " & dtimeto.Value.ToString("HH':'mm") & "# and [dateinput] >= #" & ddatefrom.Value.ToString("yyyy'/'MM'/'dd") & " " & dtimefrom.Value.ToString("HH':'mm") & "#"