使用sql在access数据库中的两个日期之间进行搜索

时间:2015-07-29 11:30:48

标签: vb.net search

这是我在访问数据库2010中搜索的代码。我的问题是,当我在两个datetimepicker之间搜索datagridview中的结果是错误的时,我的意思是当我从5月到6月之间的特定记录中搜索时,它也显示我从2月开始的记录

Private Sub Search_Record()
    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim da As New OleDbDataAdapter
    Dim dt As New DataTable
    Dim sSQL As String = String.Empty

    Dim bookdetials As New frmContactDetails

    Try
        'get connection string declared in the Module1.vb and assing it to conn variable
        conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text

        sSQL = "SELECT contact_id, first_name , birth_date, book_num, send_from, no_answer, no_answer_by, rec, book_answer_name, book_answer_num, send_date, send_to, project_name FROM tblAddressBook"

        If CheckBox1.Checked = True Then
            sSQL = sSQL & " where project_name like '%" & Me.TextBox2.Text & "%' " & _
        " AND birth_date between '" & DateTimePicker1.Text & "' AND '" & DateTimePicker2.Text & "'"
        End If

        cmd.CommandText = sSQL
        da.SelectCommand = cmd

        da.Fill(dt)

        Me.dtgResult.DataSource = dt
        Label4.Text = dt.Rows.Count

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try
End Sub

2 个答案:

答案 0 :(得分:0)

datepicker文本应该在sql中转换为datetime格式

答案 1 :(得分:0)

我遇到了同样的问题,解决方案太傻了但是有效 在db中使用text而不是datetime 确保datetimepicker输入“短格式”数据