如何过滤大于用户输入且低于用户输入的日期行

时间:2016-07-03 11:14:56

标签: mysql vb.net

我有journals_data表,其中包含:

ID  |  Name    | Input_date
001 | Example  | 02/06/2016
002 | Example2 | 15/06/2016
003 | Example3 | 02/06/2016

我有一个vb.net表单来表示用户输入。

Select Period

       Month :       Year :
From : <cboMonth>    <cboYear>
To   : <cboMonth>    <cboYear>

<OKButton> <CancelButton>

<cboMonth>个项目包含JanuaryDesember的月份名称string

<cboYear>项目包含Input_datejournals_data列的年份。

我想从journals_data中选择所有行,其中月份大于表单中的“From-Month”值,并且低于“To-Month”值,也是年份。

我在sql代码中遇到问题,其中月份的用户输入值为string(如January)而不是01

我使用DateTime.ParseExact()ToString("00")技术将January转换为01并将其与<cboYear>值相结合并添加01 }和31作为日期值。但它不起作用,它会显示journals_data中的所有行,而不会过滤Input_date

我还有其他方法可以解决我的问题吗?

1 个答案:

答案 0 :(得分:0)

我正在努力找出该怎么做,而且我得到了这个并且它有效!

' To indicates the first day of the month
Dim dayFrom As String = "01"
' To indicates the last day of its month I count the days of its month
Dim dayTo As String
dayTo = DateTime.DaysInMonth(CInt(cboYear.Text), Month(CDate(cbomonthFrom.Text + cboYear.Text)).ToString("00"))
Connect()
DA = New OleDbDataAdapter("select * from journals_data where Input_date >= #" & _
                              Format(DateTime.ParseExact(dayFrom + " " + cbomonthFrom.Text + " " + cboYear.Text, "dd MMMM yyyy", CultureInfo.InvariantCulture), "dd/MM/yyyy") & "# and Input_date <= #" & _
                              Format(DateTime.ParseExact(dayTo + " " + cbomonthTo.Text + " " + cboYear.Text, "dd MMMM yyyy", CultureInfo.InvariantCulture), "dd/MM/yyyy") & "#", Conn)
DS = New DataSet
DS.Clear()
DA.Fill(DS, "journals")
dgv.DataSource = DS.Tables("journals")
Conn.Close()

我正在使用MS-Access数据库。

感谢你们的所有提示。