SQL语法错误,非布尔类型的表达式

时间:2016-07-26 08:39:26

标签: vb.net sql-server-2008

此代码应该创建一个事件中通过销售门票赚取的收入图。

代码只有在返回错误时才会执行da.Fill(ds),这可以在代码末尾看到。

有谁知道为什么

  Private Sub frmRevenue_Load(sender As Object, e As EventArgs) Handles Me.Load

    frmMDI.addFormToCMS()

    Dim dt As DataTable
    dt = New DataTable
    dt.Columns.Add("Fee")

    Dim sales As Integer = 0

    Dim gridtable As New DataTable
    gridtable.Columns.Add("Month")
    gridtable.Columns.Add("Total")
    gridtable.Columns.Add("#")

    For i = 1 To 12
        sql = "SELECT Fee FROM tblTickets WHERE MONTH(DatePurchased) = " & i & " AND (YEAR(DatePurchased) = " & Today.Year & " OR " & Year(Today.AddYears(1)) & ") AND (Status = 'SOLD' OR RESERVED" _
& " = 'AWAITING CONFIRMATION' OR Status = 'AVAILABLE' OR Status = 'AWAITING PAYMENT');"

        da = New OleDb.OleDbDataAdapter(sql, con)
        ds = New DataSet
        da.Fill(ds)

        Dim dr As DataRow

        For Each dr In ds.Tables(0).Rows
            monthly(i) = monthly(i) + 1
            contracts = sales + 1
            total(i) = total(i) + dr.Item("Fee")
            yearlytotal = yearlytotal + dr.Item("Fee")
        Next
    Next

    For i = 1 To 12
        Dim month As String
        Select Case i
            Case 1
                month = "Jan"
            Case 2
                month = "Feb"
            Case 3
                month = "Mar"
            Case 4
                month = "Apr"
            Case 5
                month = "May"
            Case 6
                month = "Jun"
            Case 7
                month = "Jul"
            Case 8
                month = "Aug"
            Case 9
                month = "Sep"
            Case 10
                month = "Oct"
            Case 11
                month = "Nov"
            Case 12
                month = "Dec"
            Case Else
                month = "ERR"
        End Select

        gridtable.Rows.Add(month, FormatCurrency(total(i)), monthly(i))
    Next

    ugTickets.DataSource = gridtable
    ugTickets.DisplayLayout.Bands(0).Columns("Month").Width = 35
    ugTickets.DisplayLayout.Bands(0).Columns("#").Width = 20
    ugTickets.DisplayLayout.Override.AllowUpdate = DefaultableBoolean.False
    txtAnnual.ReadOnly = True
    txtAnnual.BackColor = Color.White
    txtAnnualContracts.ReadOnly = True
    txtAnnualContracts.BackColor = Color.White
    chRevenue.Titles("chTitle").Text = "Predicted revenue for " & Today.Year & " - " & Year(Today.AddYears(1))
    txtAnnual.Text = FormatCurrency(yearlytotal, 2)
    txtAnnualContracts.Text = contracts
    chRevenue.Series("Series1").Name = "Revenue"

    For i = 1 To 12
        chRevenue.Series("Revenue").Points.AddY(total(i))
    Next

    Try
        chRevenue.BackColor = Color.Transparent
        chRevenue.Legends("Revenue").BackColor = Color.Transparent
        chRevenue.Series("Revenue").ChartArea = "ChartArea1"
        chRevenue.Series("Revenue").Color = Color.SkyBlue
        chRevenue.Series("Revenue").ToolTip = FormatCurrency("#VALY", 2)
    Catch
    End Try

End Sub
  

在预期条件的上下文中指定的非布尔类型的表达式,接近'。'。

2 个答案:

答案 0 :(得分:0)

问题在于SQL的这一点:

(YEAR(DatePurchased) = " & Today.Year & " OR " & Year(Today.AddYears(1)) & ")

应该是

(YEAR(DatePurchased) = " & Today.Year & " OR YEAR(DatePurchased) = " & Year(Today.AddYears(1)) & ")

SQL(以及大多数其他语言)中的OR需要在每一侧都有两个独立有效的条件。左侧目前看起来像这样:

YEAR(DatePurchased) = 2016

......这很好。但右边看起来像这样:

2017

...这不是一个有效的布尔值。

当你在da.Fill()行中遇到这样的错误时(即在数据库中实际运行SQL的行),调试它的最简单方法是打印出值" sql"变量

通常,您只需查看它生成的SQL,问题就很明显了。其他时候你必须复制它并直接对你的数据库运行它,看看问题是什么。

答案 1 :(得分:-1)

可能是你的SQL,试试:

"SELECT Fee FROM tblTickets WHERE MONTH(DatePurchased) = '" & i &
"' AND (YEAR(DatePurchased) = '" & Today.Year & 
"' OR '" & Year(Today.AddYears(1)) & "') " & 
"AND (Status = 'SOLD' OR RESERVED = 'AWAITING CONFIRMATION' " &
                      "OR Status = 'AVAILABLE' " &
                      "OR Status = 'AWAITING PAYMENT');"