如何使用 datetimepicker 在 vb.net 中查看两个日期之间的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20261495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:58:39  来源:igfitidea点击:

How to view data between two dates in vb.net using the datetimepicker

vb.net

提问by user3045019

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim cn As New SqlConnection
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim dfrom As DateTime
    Dim dto As DateTime
    Dim da As New SqlDataAdapter

    dfrom = dtpicker1.Text
    dto = dtpicker2.Text

    cn.ConnectionString = "Data Source=JMI-PC\SQLEXPRESS;Initial Catalog=student_system;User Id=ian;Password=rockstar"
    cn.Open()
    Dim str As String
    Format(dtpicker1.Text, "yyyy-MM-dd")
    Format(dtpicker2.Text, "yyyy-MM-dd")
    str = "select Exam_Date from class1  where Exam_Date= '" & dtpicker1.Text & "' and Exam_Date='" & dtpicker2.Text & "'"

    da = New SqlDataAdapter(str, cn)
    da.Fill(dt)
    DataGridView1.DataSource = dt
    DataGridView1.DataSource = dt


End Sub

i am trying to view data between two dates using the datetimepicker and when i try to run this code i get an error aying " Conversion failed when converting date and/or time from character string." if anyone can show me how its done, i would really appreciate it

我正在尝试使用 datetimepicker 查看两个日期之间的数据,当我尝试运行此代码时,出现错误“从字符串转换日期和/或时间时转换失败。” 如果有人可以告诉我它是如何完成的,我将不胜感激

回答by liquidsnake786

You need to check your date range in your SQL statment

您需要在 SQL 语句中检查日期范围

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Private Sub Button2_Click(sender As Object, e As EventArgs) 处理Button2.Click

Dim cn As New SqlConnection
Dim ds As New DataSet
Dim dt As New DataTable
Dim dfrom As DateTime = dtpicker1.Value
Dim dto As DateTime = dtpicker2.Value


cn.ConnectionString = "Data Source=JMI-PC\SQLEXPRESS;Initial Catalog=student_system;User Id=ian;Password=rockstar"
cn.Open()
Dim str As String = "select Exam_Date from class1  where Exam_Date >= '" & Format(dFrom, "MM-dd-yyyy") & "' and Exam_Date <='" & Format(dto, "MM-dd-yyyy") & "'"

Dim da As SqlDataAdapter = New SqlDataAdapter(str, cn)
da.Fill(dt)
DataGridView1.DataSource = dt

End Sub

结束子