简体   繁体   中英

VB.net SUM :error Index was outside the bounds of the array

I have a problem with the SQL SUM function

The error I got:

Index was outside the bounds of the array.

My code:

SQLcmd = New SqlCommand("SELECT SUM(CAST(distance AS Numeric(10, 1))) FROM route WHERE id BETWEEN '" & op.departure_id & "'AND '" & op.arrival_id & "'", SQLCon)

Dim r As SqlDataReader = SQLcmd.ExecuteReader

    While r.Read()
        distance = r(3).ToString()
    End While

The error is on the distance = r(3).ToString()

This query selects a single column - SUM(CAST(distance AS Numeric(10, 1))) . You should access it with index 0 , not 3 :

While r.Read()
    distance = r(0).ToString()
    ' Use distance somehow...
End While

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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