【问题标题】:Query selecting month only in date仅在日期中查询选择月份
【发布时间】:2016-01-15 22:46:41
【问题描述】:

我有一个查询,它将根据月份从我的数据库中选择所有记录。例如,我想选择一月份的所有记录。 month() 函数对我不起作用。 ComboBox 的值是月份名称(“January”、“February”等)。我使用的是 VB 2010,我的数据库是 Microsoft Access。

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

【问题讨论】:

  • 你的组合框是否包含从 1 到 12 的月份数字?
  • 没有组合框包含完整的月份名称。

标签: sql vb.net ms-access-2010


【解决方案1】:

好吧,假设您的组合框项目按每月顺序(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 属性是一个整数,它告诉您当前选定项的索引。此属性从零开始,因此加一与 VBA Month 函数返回的数字相匹配

当然,这意味着您在此行之前有一个检查,通知您的用户从组合框中选择某些内容,并且组合框本身应该将 DropDownStyle 设置为 ComboBoxStyle.DropDownList 以避免用户输入自己的 '月'

警告:虽然在这种情况下(将整数转换为字符串)并没有过多担心 Sql 注入,但最好不要沉迷于这些做法并始终使用参数化查询。

【讨论】:

    【解决方案2】:

    诚然,这与其他响应没有什么不同,但希望表达在执行查询之前检查所选索引以及最佳使用参数,如下所示。这是 OleDb 提供者,同样适用于所有托管数据提供者,只需更改为正确的,例如SQL server 使用 SqlClient 等。

    加载组合框

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

    运行语句示例

    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
    

    【讨论】:

      【解决方案3】:

      Month() 函数返回月份数,因此您的 where 条件失败。

      改为使用like,

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

      也尝试在 where 条件中使用 Like 语句而不是 equals,因为您可能会遇到字符大小写的问题。更多讨论在Like vs (=)

      希望这会有所帮助。

      希望这会有所帮助。

      【讨论】:

      • Access/JET中没有DateName函数。
      【解决方案4】:

      你也可以使用:

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

      【讨论】:

      • 我有这个疑问,但现在我很确定虽然“MonthName”是一个有效的 VBA 函数,但它在 OleDb 上下文中不可用。我得到一个“表达式中的未定义函数 MonthName”在测试代​​码中。 See this q/a
      • @Steve:这可能是真的(我没有通过文字 SQL)。如果是这样,您将留下组合框的索引,因为您已经显示了哪种方法,顺便说一下,我随时都喜欢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多