简体   繁体   English

计算两个日期之间的条目总数

[英]Calculating total number of entries between two dates

I have a webform that queries an Access database for journal entries and gives various statistics about them; 我有一个Web表单,可以在Access数据库中查询日记条目,并提供有关日记条目的各种统计信息。 such as giving me the total number of journal entries and total number of journal entries for a specific topic. 例如提供特定主题的日记条目总数和日记条目总数。 What I would like is to be able to enter a start date and an end date and the total number of journal entries that fall between those two dates would be calculated. 我想要的是能够输入开始日期和结束日期,然后计算介于这两个日期之间的日记帐分录总数。

So far I have two textboxes with a jquery calendar but when two dates are selected and the search button is clicked, it's not picking up the right number of journal entry totals and returns 0. 到目前为止,我有两个带有jQuery日历的文本框,但是当选择两个日期并单击搜索按钮时,它没有选择正确的日记帐分录总数并返回0。

The datetime picker puts the date into a format like this: mm/dd/yyyy and the Access database has the JournalDate field formatted as Date/Time. datetime选择器将日期输入为以下格式:mm / dd / yyyy,并且Access数据库的JournalDate字段格式设置为Date / Time。 In other words, the Access database holds the dates as 4/8/2014, and the DataTime Picker displays them in the textbox as 04/08/2014. 换句话说,Access数据库保存的日期为4/8/2014,而DataTime Picker在文本框中显示的日期为04/08/2014。 I thought converting them with parse would do the trick but it still returns 0 for the total number of journal entries when dates are selected. 我以为用解析转换它们会达到目的,但是当选择日期时,它仍然为日记帐分录总数返回0。 Any help would be very much appreciated. 任何帮助将不胜感激。

It's also open to SQLInjection right now. 它现在也对SQLInjection开放。

DateTime StartDate = DateTime.Parse(txtStartDate.Text);
DateTime EndDate = DateTime.Parse(txtEndDate.Text);

using (OleDbConnection con = new OleDbConnection(constr))
    using (OleDbCommand com = new OleDbCommand("SELECT * FROM JournalEntries WHERE JournalDate BETWEEN " + txtStartDate.Text + " and " + txtEndDate.Text, con))
    {
        con.Open();
        using (OleDbDataReader myReader = com.ExecuteReader())
        {
            DataTable dt = new DataTable();
            dt.Load(myReader);
            int count = dt.Rows.Count;
            JournalEntryTotal.Text = count.ToString();
        }
    }

Most likely it's a date conversion issue. 最有可能是日期转换问题。

I don't think it's generally a good practice to parse date time string fields without explicitly providing a format. 我认为解析日期时间字符串字段而不显式提供格式通常不是一个好习惯。 It could be for example that your txtStartDate is 04/08/2014 for April 08 2014 and txtEndDate is 05/03/2014 for May 03 2014 and it makes sense to search for records between these dates, but if the default format is dd/MM/yyyy you will get August 04 2014 and March 05 2014 for the start date and end date respectively, and searching for data between those dates yields 0 results. 例如,对于2014年4月8日,您的txtStartDate为04/08/2014,对于2014年5月3日,您的txtEndDate为05/03/2014,则可以在这些日期之间搜索记录,但默认格式为dd / MM / yyyy,您将分别获得2014年8月4日和2014年3月5日的开始日期和结束日期,搜索这些日期之间的数据将得到0个结果。

Similarly, in the sql query you're running you should use a date conversion function instead of relying on a default format. 同样,在运行的sql查询中,应使用日期转换功能,而不要依赖默认格式。 So it should read something like this (I am using an oracle date conversion function but there should be something similar in access): 因此,它应该读取如下内容(我使用的是oracle日期转换功能,但在访问中应该有类似的内容):

"SELECT * FROM JournalEntries WHERE JournalDate BETWEEN to_date('" + txtStartDate.Text + "', 'mm/dd/yyyy') and to_date('" + txtEndDate.Text + "', 'mm/dd/yyyy')"

And another word about date fields and formats. 关于日期字段和格式的另一句话。 Dates don't have formats. 日期没有格式。 A date represents a specific instant in time. 日期代表特定的时间点。 August 10 2014 10:23:05 EST is the same date no matter how you write it. 2014年8月10日10:23:05 EST是相同的日期,无论您如何编写。 When you write it you use a format to display it so that whoever reads it understands exactly what instant in time you are referring to. 编写它时,您使用一种格式来显示它,以便任何人阅读它都可以准确地理解您指的是哪个时刻。

I'd first tune the query itself ie in MS SQL maangement studio and then format the date to correct format, ie: 我首先要调整查询本身,即在MS SQL maangement Studio中,然后将日期格式化为正确的格式,即:

Format(Me.DateTimePicker1.Value, "MM/dd/yyyy")

If you tried, I suspect that your problem is in the upper/lower case of the format string. 如果尝试过,我怀疑您的问题出在格式字符串的大写/小写形式。 Note, that month is upper case, while the rest is lower case (important!). 请注意,该月份为大写字母,其余月份为小写字母(重要!)。 Not like what you wrote above: mm/dd/yyyy. 不像您上面写的:mm / dd / yyyy。

Regards, 问候,

Libor 伦敦银行同业拆借利率

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM