2013-03-04 16 views
0

我从表中读取数据时遇到问题。这是我的编码无法读取多于一行的数据

Dim SqlQry As String = "SELECT Distinct(RechargeAmt) from KioskItem where TelcoID='" & TelcoID & "'" 
     Dim SqlCmd As New SqlCommand(SqlQry, Conn) 
     Dim dr As SqlDataReader = SqlCmd.ExecuteReader 
     dr.Read() 
     If dr.HasRows Then 
      While dr.Read() 
       Dim SqlQry As String = "SELECT Distinct(RechargeAmt) from KioskItem where TelcoID='" & TelcoID & "'" 
     Dim SqlCmd As New SqlCommand(SqlQry, Conn) 
     Dim dr As SqlDataReader = SqlCmd.ExecuteReader 
     dr.Read() 
     If dr.HasRows Then 
      While dr.Read() 
       RechargeAmt = dr(0) 
       a = a & RechargeAmt & ";" 
      End While 
     End If 
     If a = 0 Then 
      Return RechargeAmt 
     Else 
      Return a 
     End If   

我的问题是:如果数据有多个行,它没有任何问题的数据保存到一个字符串,但是当我的表中的数据只有一个rowm,它是在样品无法读取数据。我尝试这个

If dr.HasRows Then 
      RechargeAmt = dr(0) 
      While dr.Read() 
       a = a & RechargeAmt & ";" 
      End While 
     End If 
     If a = 0 Then 
      Return RechargeAmt 
     Else 
      Return a 
     End If  

,但它仍然无法从表

回答

1

那是因为你总是缺少的第一行读取数据。

看到这里

dr.Read()    //Gets the first row 
    If dr.HasRows Then 
     While dr.Read() //Gets the second row onwards 

你总是跳过第一行。

初始读取不是必需的。你可以用

While dr.Read() 

控制直接启动会去只如果有行内循环。