这是一个简单的函数,如果用户存在于users表中,则返回TRUE,否则返回false.
由于某种原因,它总是返回false,我甚至删除了WHERE子句,我仍然得到错误?(手动检查查询分析器告诉我有很多行?)
Public Shared Function DoesUserExist(ByVal userID As Integer) As Boolean
Dim retValue As Boolean
retValue = False
Using conn As New SqlConnection(GetConnectionString())
'Dim cmd As SqlCommand = New SqlCommand("SELECT user_ID FROM users WHERE user_ID = @userID", conn)
Dim cmd As SqlCommand = New SqlCommand("SELECT user_ID FROM users", conn)
cmd.Parameters.Add("@userID", SqlDbType.NVarChar).Value = userID
cmd.CommandType = CommandType.Text
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
'If Not reader Is Nothing Then
' HttpContext.Current.Response.Write("<br>Null")
'End If
If reader.Read() Then
retValue = True
End If
conn.Close()
cmd.Dispose()
End Using
retValue = False
Return retValue
End Function
Run Code Online (Sandbox Code Playgroud)
您在退出之前将retValue设置为false.那就是问题所在.
这是正确的代码:
...
If reader.Read() Then
retValue = True
Else
retValue = False
End If
conn.Close()
cmd.Dispose()
End Using
Return retValue
....
Run Code Online (Sandbox Code Playgroud)