简体   繁体   English

查询仅在日期中选择月份

[英]Query selecting month only in date

I have a query that will select all records from my database based on the month. 我有一个查询,它将根据月份从数据库中选择所有记录。 For example, I want to select all records for the month of January. 例如,我要选择一月份的所有记录。 The month() function doesn't work for me. month()函数对我不起作用。 The ComboBox has a value of month name ("January", "February", etc.). ComboBox的值是月份名称(“ January”,“ February”等)。 I'm using VB 2010 and my database is Microsoft Access. 我正在使用VB 2010,数据库是Microsoft Access。

query = "SELECT empid, empname, department, empdate, timeinam, " & _
        "timeoutam, lateam, timeinpm, timeoutpm, latepm, thw " & _
        "FROM tbldtr where Month(empdate) =" & cmbMonth.Text

Well, supposing that you have your combobox items sorted in a monthly order (Jan, Feb, March...) then you could write your query as 好吧,假设您的组合框项目按月顺序排序 (1月,2月,3月...),则可以将查询写为

query = "SELECT empid, empname, department, empdate, timeinam, " & _
        "timeoutam,lateam, timeinpm, timeoutpm,latepm,thw " & _
        "FROM tbldtr where Month(empdate) =" & cmbMonth.SelectedIndex + 1

ComboBox.SelectedIndex property is an integer that tells you the index of the current selected item. ComboBox.SelectedIndex属性是一个整数,它告诉您当前所选项目的索引。 This property starts at zero, so adding one, matches the number returned by the VBA Month function 此属性从零开始,因此加一,与VBA Month函数返回的数字匹配

Of course, this means that you have somewhere, before this line, a check that informs your user to select something from the combobox and the combobox itself should have DropDownStyle set to ComboBoxStyle.DropDownList to avoid user inputs its own 'month' 当然,这意味着您需要在此行之前的某处进行检查,以通知您的用户从组合框中选择内容,并且组合框本身应将DropDownStyle设置为ComboBoxStyle.DropDownList以避免用户输入自己的“ month”

WARNING: While, in this context, (converting an integer to a string) there is no much concern about Sql Injection it is better to not indulge in these practices and use always a parameterized query. 警告:尽管在这种情况下(将整数转换为字符串)没有太多关于Sql Injection的担忧,但最好不要沉迷于这些做法并始终使用参数化查询。

Admittedly this is no different than other responses but wanted to express checking the selected index before executing the query along with best to use parameters as shown below. 诚然,这与其他响应没有什么不同,但希望在执行查询之前表示对所选索引的检查以及最佳使用参数,如下所示。 This is OleDb provider, same applies for all managed data providers just change to the correct one eg SQL server use SqlClient etc. 这是OleDb提供程序,同样适用于所有托管数据提供程序,只需更改为正确的一个即可,例如SQL Server使用SqlClient等。

Load ComboBox 加载组合框

cmbMonth.Items.AddRange(
(
    From M In System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames
    Where Not String.IsNullOrEmpty(M)).ToArray
)

Sample to run statement 样品运行语句

If cmbMonth.SelectedIndex > -1 Then
    Using cn As New OleDb.OleDbConnection With
        {
            .ConnectionString = "Your connection string"
        }
        Using cmd As New OleDb.OleDbCommand With
            {
                .Connection = cn,
                .CommandText =
                    "SELECT empid, empname, department, empdate, timeinam, timeoutam, lateam, timeinpm, timeoutpm, latepm, thw " &
                    "FROM tbldtr where Month(empdate) = @SelectedMonth"
            }
            cmd.Parameters.Add(New OleDb.OleDbParameter With
                {
                    .ParameterName = "@SelectedMonth",
                    .DbType = DbType.Int32,
                    .Value = cmbMonth.SelectedIndex + 1
                }
            )
            ' continue
        End Using
    End Using
End If

Month() function returns the Month number ,so your where condition fails. Month()函数返回Month数,因此您的where条件失败。

instead use like, 代替使用

query = "SELECT empid, empname, department, empdate, timeinam, timeoutam,lateam, timeinpm, timeoutpm,latepm,thw FROM tbldtr where datename(month, empdate) =" & cmbMonth.Text

also try to use Like statement rather than equals in where condition,because you may encounter issues with Character casing.more discussion on Like vs (=) . 也请尝试使用Like语句而不是在条件中使用equal语句,因为您可能会遇到字符框问题。有关Like vs(=)的更多讨论。

hope this helps. 希望这可以帮助。

hope this helps. 希望这可以帮助。

您还可以使用:

"FROM tbldtr where MonthName(Month(empdate)) ='" & cmbMonth.Text & "'"

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

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