如何在 vb.net 中获取两个日期之间的记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14555324/
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 12:03:18  来源:igfitidea点击:

How to get records between two dates in vb.net?

vb.netms-access

提问by Aniruddh

All ...

全部 ...

I need to display records that are between the two dates passed in from DateTimePickers.

我需要显示从 DateTimePickers 传入的两个日期之间的记录。

I am getting records that are NOT in between the dates that I specified from vb.net.

我得到的记录不在我从 vb.net 指定的日期之间。

Please go through the code shown below....

请通过下面显示的代码......

Following is the code :

以下是代码:

Private Sub btn_Show_Inquiry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Show_Inquiry.Click
    report_viewer_form.Report_viewer_CrystalReportViewer1.ReportSource = Nothing
    report_viewer_form.Report_viewer_CrystalReportViewer1.Refresh()
    str1 = "SELECT * FROM Inquiry_Details WHERE Inquiry_Date>=#" & dtp_inq_from.Text & "# AND Inquiry_Date<=#" & dtp_inq_to.Text & "#"

    If dtp_inq_from.Text > dtp_inq_to.Text Then
        MessageBox.Show("FROM_DATE Must Be Less Then TO_DATE.", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Exit Sub
    End If
    If cn.State <> ConnectionState.Open Then
        cn.Open()
    End If
    da = New OleDbDataAdapter(str1, cn)

    report_dataset = New DataSet
    da.Fill(report_dataset, "table2")
    If MsgBox("Do You Want to Print Report ?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
        report_viewer_form.Show()
        Dim cr As New ReportDocument
        cr = New Inquiry_CrystalReport
        cr.SetDataSource(report_dataset.Tables("table2"))
        report_viewer_form.Report_viewer_CrystalReportViewer1.ReportSource = cr
    End If
End Sub

回答by Wanka-Wah-Wah

Your problem seems to be in the way you parse passed date parameters to the query and they're probably not in format that Access would recognize as a valid Date type. Try with CDate()function to let Access parse your input values to its internal date type properly. It will accept any valid date expression:

您的问题似乎在于您解析传递给查询的日期参数的方式,并且它们的格式可能不是 Access 会识别为有效日期类型的格式。尝试使用CDate()函数让 Access 将您的输入值正确解析为其内部日期类型。它将接受任何有效的date expression

Any expression that can be interpreted as a date, including date literals, numbers that look like dates, strings that look like dates, and dates returned from functions. A date expression is limited to numbers or strings, in any combination, that can represent a date from January 1, 100 – December 31, 9999.

任何可以解释为日期的表达式,包括日期文字、看起来像日期的数字、看起来像日期的字符串以及从函数返回的日期。日期表达式仅限于数字或字符串的任意组合,可以表示 100 年 1 月 1 日至 9999 年 12 月 31 日之间的日期。

Your code could thus look like this:

因此,您的代码可能如下所示:

str1 = "SELECT * FROM Inquiry_Details WHERE Inquiry_Date>=CDate('" & 
       dtp_inq_from.Text & "') AND Inquiry_Date<=CDate('" & 
       dtp_inq_to.Text & "')"

Another function that you might want to try (if CDatewon't cut it) is DateValue():

您可能想尝试的另一个功能(如果CDate不会削减它)是DateValue()

The required date argument is normally a string expression representing a date from January 1, 100 through December 31, 9999. However, date can also be any expression that can represent a date, a time, or both a date and time, in that range.

所需的日期参数通常是一个字符串表达式,表示从 100 年 1 月 1 日到 9999 年 12 月 31 日之间的日期。但是,日期也可以是可以表示该范围内的日期、时间或日期和时间的任何表达式.

The success of these two functions might also depend on input date formatting and system locale.

这两个函数的成功还可能取决于输入日期格式和系统区域设置。