[英]Looping through SQLdatareader and comparing row values
I have a function that calls a stored procedure. 我有一个调用存储过程的函数。 I need to compare the result to an input parameter.
我需要将结果与输入参数进行比较。 I keep getting a warning that the function does not return on all code paths, but I can't figure out where.
我不断收到警告,该函数不会在所有代码路径上返回,但是我不知道该在哪里。
So, 2 questions: 1. Am I looping through the SqlDataReader
correctly or should I fill a datatable with my reader results? 因此,有两个问题:1.我是否正确地遍历
SqlDataReader
,还是应该用阅读器结果填充数据表? 2. Where am I missing a return value? 2.我在哪里缺少返回值?
Function code: 功能码:
Function FZCheck(FZ As String) As Boolean
Dim constr As String = My.Settings.devTOD.ToString
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand("spSelectFloodZones", con)
cmd.CommandType = CommandType.StoredProcedure
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim rdr As SqlDataReader
rdr = cmd.ExecuteReader
If rdr.HasRows Then
Do While rdr.Read
If String.Equals(rdr(0).ToString, FZ) = True Then
Return True
Else
Return False
End If
Loop
Else
Return False
End If
If con.State = ConnectionState.Open Then
con.Close()
End If
rdr.Close()
End Function
Stored proc code: (very simple) 存储的过程代码:(非常简单)
ALTER PROCEDURE [dbo].[spSelectFloodZones]
AS
SET NOCOUNT ON
SET ROWCOUNT 0
-- ====================
-- Select flood zones
-- ====================
SELECT DISTINCT FloodZone
FROM TOD.dbo.FloodZones
Language is VB.NET, using SQL Server 2012. 语言是VB.NET,使用SQL Server 2012。
Here you go with some minor refactoring. 在这里,您需要进行一些小的重构。
Function FZCheck(FZ As String) As Boolean
Dim constr As String = My.Settings.devTOD.ToString
Dim con As New SqlConnection(constr)
Dim result As Boolean = False
Dim cmd As New SqlCommand("spSelectFloodZones", con)
cmd.CommandType = CommandType.StoredProcedure
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim rdr As SqlDataReader
rdr = cmd.ExecuteReader
If rdr.HasRows Then
Do While rdr.Read
If String.Equals(rdr(0).ToString, FZ) = True Then
result = True
End If
Loop
End If
If con.State = ConnectionState.Open Then
con.Close()
End If
rdr.Close()
Return result
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.