[英]VB.net SUM :error Index was outside the bounds of the array
I have a problem with the SQL SUM function 我的SQL SUM函数有问题
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()
错误是在
distance = r(3).ToString()
This query selects a single column - SUM(CAST(distance AS Numeric(10, 1)))
. 此查询选择单个列
SUM(CAST(distance AS Numeric(10, 1)))
。 You should access it with index 0
, not 3
: 您应该使用索引
0
而不是3
:
While r.Read()
distance = r(0).ToString()
' Use distance somehow...
End While
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.