舍入数据时出现 IndexOutOfRangeException

IndexOutOfRangeException when rounding off data

我有以下stored procedure

Create Procedure [dbo].[spVerifyCustomerUserLogin]    

@EmbossLine varchar(16),    

@password varchar(50)    

as    

    Select C.EmbossLine,C.EmbossName,round(CB.TotalLoading,2),round(CB.Totalredemption,2),round(CB.CardBalance,2) 
    from Card C inner join CardBalance CB on CB.PAN=C.EmbossLine

    where 

    EmbossLine = @EmbossLine 

    and [password] = @password 

    and Status='E0'

我有以下 VB.Net 代码来访问它:

Dim UserData As New ArrayList
            Dim dr As SqlDataReader
            dr = GenericDB.ExecuteSPForDataReader("spVerifyCustomerUserLogin", spParameters)
            If dr.HasRows Then
                dr.Read()
                UserData.Add(dr.Item("EmbossLine"))
                UserData.Add(dr.Item("EmbossName"))
                UserData.Add(dr.Item("TotalLoading"))
                UserData.Add(dr.Item("Totalredemption"))
                UserData.Add(dr.Item("CardBalance"))
                dr.Close()
                Return UserData
            End If
            dr.Close()
            Return UserData

问题是我的代码在

上抛出异常 IndexOutOfRangeException
UserData.Add(dr.Item("TotalLoading"))

请注意,当我在没有 round 函数的情况下访问存储过程时,它工作正常,因此它与我的 round 函数有关。 请注意,我的字段 TotalLoadingTotalRedemptionCardBalance 是货币类型。是因为那个吗?

不,这是因为您在 totalLoading 列上使用了一个函数并且没有为其设置别名。

将存储过程中的 SELECT 语句更改为:

 Select 
  C.EmbossLine,
  C.EmbossName,
  round(CB.TotalLoading,2) as TotalLoading,
  round(CB.Totalredemption,2) as TotalRedemption,
  round(CB.CardBalance,2) as CardBalance
        from Card C inner join CardBalance CB on CB.PAN=C.EmbossLine

当您对列执行操作时(例如 round(CB.TotalLoading,2)),它不再有名称。您可以使用 AS 为其命名,例如

SELECT C.EmbossLine, C.EmbossName, ROUND(CB.TotalLoading, 2) AS 'RoundedTotalLoading',...