VB.NET 中的日期时间搜索不显示结果

发布于 2024-11-19 09:21:30 字数 593 浏览 0 评论 0原文

我的搜索表单有问题。我使用日期时间选择器供用户选择他们喜欢的日期。当我尝试设置任何日期时,它不会产生任何结果。下面是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

季末如歌 2024-11-26 09:21:30

日期不是字符串。您不应使用 LIKE 运算符。通常,您在搜索日期时需要使用 BETWEEN 运算符。

例子:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文