vb.net在查询表达式中隐含内连接语法错误(缺少运算符)

时间:2015-07-21 21:40:19

标签: sql sql-server vb.net

这是我收到异常消息的代码。但是,此代码在sql server 2005中运行良好,但在访问时生成错误。 这段代码在sql server项目中工作正常,但是正如我提到的那样访问它的生成异常..

Public Function CalculateFeeReciept(ByVal monthid As Integer) As DataTable

    Dim cmd1 As New OleDbCommand("Select * from mstFeeHead", sqlcon)
    Dim dtmstFeeHead As New DataTable 'dtmstFeeHead contains all the fee heads id's
    Dim adp1 As New OleDbDataAdapter(cmd1)
    adp1.Fill(dtmstFeeHead)
    cmd1.Dispose()
    Dim selectedmonth As Integer
    Dim feeheadid As Integer
    Dim arr(25) As Integer 'arr contains Fee head id's that should be paid in the selected month

    If monthid = 13 Then
        monthid = 1
    ElseIf monthid = 14 Then
        monthid = 2
    ElseIf monthid = 15 Then
        monthid = 3
    End If

    selectedmonth = monthid + 3
    Dim m As Integer = 0
    For j As Integer = 0 To dtmstFeeHead.Rows.Count - 1
        feeheadid = Convert.ToInt32(dtmstFeeHead.Rows(j)(0))
        If dtmstFeeHead.Rows(j)(selectedmonth) Then
            arr(m) = feeheadid
            m = m + 1
        End If
    Next

    Dim cmd2 As New OleDbCommand("SELECT txnStudentFeeHead.FeeHeadID, mstFeeHead.FeeHeadName, mstFeePlan.Amount " & _
                                 "FROM " & _
                                 "txnStudentFeeHead " & _
                                 "INNER JOIN " & _
                                 "mstFeeHead " & _
                                 "ON " & _
                                 "txnStudentFeeHead.FeeHeadID = mstFeeHead.FeeHeadID " & _
                                 "INNER JOIN " & _
                                 "mstFeePlan " & _
                                 "ON " & _
                                 "mstFeeHead.FeeHeadID = mstFeePlan.FeeHeadID " & _
                                 "WHERE " & _
                                 "txnStudentFeeHead.StudentID = @StudentID) " & _
                                 "AND " & _
                                 "(mstFeePlan.SessionID = @SessionID) " & _
                                 "AND " & _
                                 "(mstFeePlan.ClassID = @ClassID) ", sqlcon)
    cmd2.CommandType = CommandType.Text
    cmd2.Parameters.Add("@StudentID", OleDbType.Integer).Value = StudentID()
    cmd2.Parameters.Add("@ClassID", OleDbType.Integer).Value = Convert.ToInt32(cmbClass.SelectedValue)
    cmd2.Parameters.Add("@SessionID", OleDbType.Integer).Value = Convert.ToInt32(cmbSession.SelectedValue)
    Dim dt As New DataTable 'dt contains all the fee head id's that are alloted to the students
    Dim adp As New OleDbDataAdapter(cmd2)
    adp.Fill(dt)
    cmd2.Dispose()

    Dim dt2 As New DataTable 'dt2 contains all the fee head id's that are alloted to the student and that should be paid in that particular month 
    ' dt2 contains the filtrate of dt and arr
    dt2 = dt.Clone()
    For i As Integer = 0 To arr.Length - 1
        For j As Integer = 0 To dt.Rows.Count - 1
            Dim dtrow As DataRow = dt2.NewRow()
            If arr(i) = dt.Rows(j)(0) Then
                dtrow(0) = arr(i)
                dtrow(1) = dt.Rows(j)(1)
                dtrow(2) = dt.Rows(j)(2)
                dt2.Rows.Add(dtrow)
            End If
        Next
    Next

    cmd2 = New OleDbCommand("Select Sum(TotalFees) as TotalFees, Sum(LateFees) as TotalLateFees, Sum(OldBalance) as TotalOldBalance, Sum(Discount) as TotalDiscount, Sum(Scholarship) as TotalScholarship, Sum(Concession) as TotalConcession, Sum(AmountReceived) as TotalAmountReceived from txnFeePayment where SessionID=@SessionID and StudentID=@studentid and MonthID=@monthid Group by StudentId,MonthID", sqlcon)
    cmd2.CommandType = CommandType.Text
    cmd2.Parameters.Add("@studentid", OleDbType.Integer).Value = StudentID()
    cmd2.Parameters.Add("@SessionID", OleDbType.Integer).Value = cmbSession.SelectedValue

    cmd2.Parameters.Add("@monthid", OleDbType.Integer).Value = monthid
    Dim dtStudentReciept As New DataTable 'dt contains all the fee head id's that are alloted to the students
    adp = New OleDbDataAdapter(cmd2)
    adp.Fill(dtStudentReciept)
    cmd2.Dispose()

    Dim dtrow1 As DataRow = dt2.NewRow()

    If (dtStudentReciept.Rows.Count > 0) Then

        dtrow1(0) = 0
        dtrow1(1) = "Total Late Fees"
        dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(1))
        dt2.Rows.Add(dtrow1)

        dtrow1 = dt2.NewRow()
        dtrow1(0) = 0
        dtrow1(1) = "Total Discount"
        dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(3)) * -1
        dt2.Rows.Add(dtrow1)

        dtrow1 = dt2.NewRow()
        dtrow1(0) = 0
        dtrow1(1) = "Total Scholarship"
        dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(4)) * -1
        dt2.Rows.Add(dtrow1)

        dtrow1 = dt2.NewRow()
        dtrow1(0) = 0
        dtrow1(1) = "Total Concession"
        dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(5)) * -1
        dt2.Rows.Add(dtrow1)


        dtrow1 = dt2.NewRow()
        dtrow1(0) = 0
        dtrow1(1) = "Total Amount Received"
        dtrow1(2) = Convert.ToInt32(dtStudentReciept.Rows(0)(6)) * -1
        dt2.Rows.Add(dtrow1)

        Dim totalamount As Integer = 0
        For k As Integer = 0 To dt2.Rows.Count - 1
            totalamount = totalamount + dt2.Rows(k)(2)
        Next

        'dtrow1 = dt2.NewRow()
        'dtrow1(0) = totalamount
        'dtrow1(1) = "Current Month Fee"
        'dtrow1(2) = totalamount
        'dt2.Rows.Add(dtrow1)
    Else



        Dim totalamount As Integer = 0
        For k As Integer = 0 To dt2.Rows.Count - 1
            totalamount = totalamount + dt2.Rows(k)(2)
        Next

        'dtrow1 = dt2.NewRow()
        'dtrow1(0) = totalamount
        'dtrow1(1) = "Current Month Fee"
        'dtrow1(2) = totalamount
        'dt2.Rows.Add(dtrow1)

    End If



    dgvDisplay.DataSource = dt2

    For i As Integer = 0 To dgvDisplay.Columns.Count - 1
        dgvDisplay.Columns.Item(i).SortMode = DataGridViewColumnSortMode.NotSortable
        dgvDisplay.Columns(2).Width = 65
        dgvDisplay.Columns(1).Width = 132
    Next

    dgvDisplay.Columns.Item(0).Visible = False
    'txtTotalFees.Text = dt2.Rows(dt2.Rows.Count - 1)(0)
    Return dt2
End Function

2 个答案:

答案 0 :(得分:2)

您的查询存在一些问题。

  1. 一旦代码中的一行完成,你就没有留下任何空格,整个查询可能看起来像是单独的行,但它是一个没有任何空格的长字符串。

    我在每行末尾的下面一段代码中添加了空格。

    ("SELECT [txnStudentFeeHead].[FeeHeadID],[mstFeeHead].[FeeHeadName]," & _  
                                     "[mstFeePlan].[Amount] " & _
                                     "FROM " & _
                                     "[txnStudentFeeHead] " & _
                                     "INNER JOIN " & _
                                     "[mstFeeHead] " & _
    
  2. 您已将变量放在sqaure括号[]中,这意味着SQL Server会将它们视为SQL Server对象(表名,列名)名称而不是变量。删除方括号。

    "WHERE " & _
    "([txnStudentFeeHead].[StudentID] = @StudentID) " & _
    "AND" & _
    "([mstFeePlan].[SessionID] = @SessionID) " & _
    "AND" & _
    "([mstFeePlan].[ClassID] = @ClassID) ", sqlcon)
    

答案 1 :(得分:1)

经过长时间的努力和头痛解决了这个问题

Dim cmd2 As New OleDbCommand("SELECT txnStudentFeeHead.FeeHeadID, mstFeeHead.FeeHeadName, mstFeePlan.Amount, txnStudentFeeHead.StudentID, mstFeePlan.ClassID, mstFeePlan.SessionID FROM (txnStudentFeeHead INNER JOIN mstFeeHead ON txnStudentFeeHead.FeeHeadID = mstFeeHead.FeeHeadID) INNER JOIN mstFeePlan ON mstFeeHead.FeeHeadID = mstFeePlan.FeeHeadID WHERE (((txnStudentFeeHead.StudentID)=@StudentID) AND ((mstFeePlan.ClassID)=@ClassID) AND ((mstFeePlan.SessionID)=@SessionID))", sqlcon)
    cmd2.CommandType = CommandType.Text
    cmd2.Parameters.Add("@StudentID", OleDbType.Integer).Value = StudentID()
    cmd2.Parameters.Add("@ClassID", OleDbType.Integer).Value = Convert.ToInt32(cmbClass.SelectedValue)
    cmd2.Parameters.Add("@SessionID", OleDbType.Integer).Value = Convert.ToInt32(cmbSession.SelectedValue)
    Dim dt As New DataTable 'dt contains all the fee head id's that are alloted to the students
    Dim adp As New OleDbDataAdapter(cmd2)
    adp.Fill(dt)
    cmd2.Dispose()