遍历 SQLdatareader 并比较行值
Looping through SQLdatareader and comparing row values
我有一个调用存储过程的函数。我需要将结果与输入参数进行比较。我一直收到警告,该函数在所有代码路径上都没有 return,但我不知道在哪里。
那么,2 个问题:
1. 我是否正确地遍历了 SqlDataReader
还是应该用我的 reader 结果填充数据表?
2. 我哪里遗漏了 return 值?
函数代码:
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
存储过程代码:(非常简单)
ALTER PROCEDURE [dbo].[spSelectFloodZones]
AS
SET NOCOUNT ON
SET ROWCOUNT 0
-- ====================
-- Select flood zones
-- ====================
SELECT DISTINCT FloodZone
FROM TOD.dbo.FloodZones
语言是 VB.NET,使用 SQL Server 2012。
下面是一些小的重构。
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
我有一个调用存储过程的函数。我需要将结果与输入参数进行比较。我一直收到警告,该函数在所有代码路径上都没有 return,但我不知道在哪里。
那么,2 个问题:
1. 我是否正确地遍历了 SqlDataReader
还是应该用我的 reader 结果填充数据表?
2. 我哪里遗漏了 return 值?
函数代码:
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
存储过程代码:(非常简单)
ALTER PROCEDURE [dbo].[spSelectFloodZones]
AS
SET NOCOUNT ON
SET ROWCOUNT 0
-- ====================
-- Select flood zones
-- ====================
SELECT DISTINCT FloodZone
FROM TOD.dbo.FloodZones
语言是 VB.NET,使用 SQL Server 2012。
下面是一些小的重构。
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