代码之家  ›  专栏  ›  技术社区  ›  Danny James

尝试使用sqlclient时出错。sqldatareader?

  •  -1
  • Danny James  · 技术社区  · 7 年前

    在使用datareader的那一行,它说单词“And”的语法不正确。

    我尝试了许多改变,试图让它工作,但没有任何效果。有什么帮助吗?

    Dim sqlline1 As String = String.Format("Select count(debtor_id) As 
    numbernew, sum(debtor_original_debt) As valuenew, SUM(debtor_balance)
    From debtors Where debtor_cohort_id Like {0} + {2} And (debtor_client_id >= 
    {0} And debtor_client_id <= {1})", clientid_start, clientid_end, Cohort4)
    

    功能it错误:-

    Public Function populatestringwithsql(command As String) As String
        Dim conn As String = "a server connection"
        Dim i = 1
        Dim fields(15) As String
        Dim numberoffields As Integer
        Using connObj As New SqlClient.SqlConnection(conn)
            Using cmdObj As New SqlClient.SqlCommand(command, connObj)
                connObj.Open()
                Dim readobj As SqlClient.SqlDataReader = cmdObj.ExecuteReader()
                Dim schematable As DataTable = readobj.GetSchemaTable()
                readobj.Close()
    
                For Each dr As DataRow In schematable.Rows
                    numberoffields = schematable.Columns.Count
                    For Each dc As DataColumn In schematable.Columns
    
                        fields(i) = dc.ColumnName
                        i += 1
                    Next
                Next
    
                Using readerObj As SqlDataReader = cmdObj.ExecuteReader
                    'This will loop through all returned records 
                    While readerObj.Read
                        If fields(1) <> "" Then
                            fields(1) = readerObj(fields(1)).ToString
                        End If
                        If fields(2) <> "" Then
                            fields(2) = readerObj(fields(2)).ToString
                        End If
                        If fields(3) <> "" Then
                            fields(3) = readerObj(fields(3)).ToString
                        End If
                        If fields(4) <> "" Then
                            fields(4) = readerObj(fields(4)).ToString
                        End If
                        If fields(5) <> "" Then
                            fields(5) = readerObj(fields(5)).ToString
                        End If
                        If fields(6) <> "" Then
                            fields(6) = readerObj(fields(6)).ToString
                        End If
                        If fields(7) <> "" Then
                            fields(7) = readerObj(fields(7)).ToString
                        End If
                        If fields(8) <> "" Then
                            fields(8) = readerObj(fields(8)).ToString
                        End If
                        If fields(9) <> "" Then
                            fields(9) = readerObj(fields(9)).ToString
                        End If
                        If fields(10) <> "" Then
                            fields(10) = readerObj(fields(10)).ToString
                        End If
                        If fields(11) <> "" Then
                            fields(11) = readerObj(fields(11)).ToString
                        End If
                        If fields(12) <> "" Then
                            fields(12) = readerObj(fields(12)).ToString
                        End If
                        If fields(13) <> "" Then
                            fields(13) = readerObj(fields(13)).ToString
                        End If
                        If fields(14) <> "" Then
                            fields(14) = readerObj(fields(14)).ToString
                        End If
                        If fields(15) <> "" Then
                            fields(15) = readerObj(fields(15)).ToString
                        End If
                    End While
                End Using
                connObj.Close()
            End Using
        End Using
    
        Dim stringend As Text.StringBuilder
    
        For Each field In fields
            stringend.Append(field)
    
        Next
    
        Return stringend.ToString
    
    End Function
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Dave Poole    7 年前

    正在查看 Where debtor_cohort_id Like {0} + {2} And ... 看起来您正在尝试连接 client_id cohort4 . 除非client\u id或cohort4包含通配符,否则使用=而不是LIKE。

    你可能需要在你喜欢的比较中加上单引号 Where debtor_cohort_id Like '{0} + {2}' And ...

    最简单的方法是打印sqlline1变量,以准确揭示其错误。