VB.NET 中的日期时间搜索不显示结果
我的搜索表单有问题。我使用日期时间选择器供用户选择他们喜欢的日期。当我尝试设置任何日期时,它不会产生任何结果。下面是我的代码:
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
当我从日期时间选择器中选择一个日期时,它除了空白数据网格视图之外什么也不显示,即使该日期在数据库中可用。
请帮我解决这个问题。
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 pick 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
日期不是字符串。您不应使用 LIKE 运算符。通常,您在搜索日期时需要使用 BETWEEN 运算符。
例子:
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: