开发者

Date-Time searching in VB.NET displays no result

I have a problem with search form. I use date-time picker for user to pickup their prefer date. And when I tried to set any date, it produces no result. Bellow is my code:

Try
     myDataSet.Clear()
     myCommand = New SqlCommand("SELECT * FROM tblVisitor WHERE EnterDate LIKE @EnterDate", myConnection)
     myCommand.Parameters.AddWithValue("@EnterDate", DateTime.Parse(cboEnterDate.Text))
     myAdapter = New SqlDataAdapter(myCommand)
     myAdapter.Fill(myDataSet, "tblVisitor")
     dgvVisitor.DataSource = myDataSet.Tables(0)
 Catch ex As Exception
    MsgBox(ex.Message)
 End Try

And when I pic开发者_如何学Pythonk one date from the date-time picker, it displays nothing beside the blank data grid view, even that date is available in the database.

Please help me solve this problem.


Dates are not strings. You should not use the LIKE operator. Typically you will want to use the BETWEEN operator when searching for dates.

Example:

Try
     myDataSet.Clear()
     myCommand = New SqlCommand("SELECT * FROM tblVisitor WHERE EnterDate BETWEEN @EnterDate AND DATEADD(dd, 1, @EnterDate)", myConnection)
     myCommand.Parameters.AddWithValue("@EnterDate", DateTime.Parse(cboEnterDate.Text))
     myAdapter = New SqlDataAdapter(myCommand)
     myAdapter.Fill(myDataSet, "tblVisitor")
     dgvVisitor.DataSource = myDataSet.Tables(0)
 Catch ex As Exception
    MsgBox(ex.Message)
 End Try
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜