为什么我的 reader 陷入无限循环
Why is my reader stuck in infinite loop
谁能解释一下为什么会陷入死循环?我把它放在一个按钮下面,效果很好。但是当我把它放在 class 中并实例化 class 时,它陷入了无限循环。我假设它卡在了循环中,因为当我添加断点时,它不会超出循环内部。它甚至没有通过 "End While",但如果我将此代码放在按钮下,单击 ASP.NET 它就可以正常工作。我究竟做错了什么?
请不要批评 "SQL injection" 我将值传递到数据库的方式,因为这不会成为 public 的事情而且我很清楚那。
Imports System.Data
Imports System.Data.SqlClient
'Step 1: Select the device ID
Function SelectDevice(d As String, b As String, m As String, o As String) As String
' Try
'Connect to database
Dim xcitoDBConnection As SqlConnection
xcitoDBConnection = New SqlConnection("Data Source=NOTDISPLAYED;" + "Initial Catalog=NOTDISPLAYED;" + "Integrated Security=True")
Dim GetDeviceID As String = "SELECT DISTINCT(Identifier) FROM Support.Devices WHERE DeviceType='" & d & "' AND Brand = '" & b & "' AND Model = '" & m & "' AND OS = '" & o & "'"
Dim command As SqlCommand = New SqlCommand(GetDeviceID, xcitoDBConnection)
xcitoDBConnection.Open()
'command.ExecuteScalar()
Dim reader As SqlDataReader
reader = command.ExecuteReader(CommandBehavior.CloseConnection)
Dim GetID As String
While reader.Read()
GetID = reader("Identifier").ToString
Return GetID
End While
xcitoDBConnection.Close()
' Catch ex As ApplicationException
' ex.Source = ("Selection Error")
' Throw ex
' Finally
'End Try
End Function
End Class
写
reader = command.ExecuteReader()
而不是
reader = command.ExecuteReader(CommandBehavior.CloseConnection)
因为您在 return 之前关闭了连接。
并且 return 函数结束
关注这个
Public Sub CreateCommand(ByVal queryString As String, _
ByVal connectionString As String)
Dim GetID As String
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As New SqlCommand(queryString, connection)
Dim reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
GetID = reader(0).ToString
End While
End Using
Return GetID
End Sub
谁能解释一下为什么会陷入死循环?我把它放在一个按钮下面,效果很好。但是当我把它放在 class 中并实例化 class 时,它陷入了无限循环。我假设它卡在了循环中,因为当我添加断点时,它不会超出循环内部。它甚至没有通过 "End While",但如果我将此代码放在按钮下,单击 ASP.NET 它就可以正常工作。我究竟做错了什么?
请不要批评 "SQL injection" 我将值传递到数据库的方式,因为这不会成为 public 的事情而且我很清楚那。
Imports System.Data
Imports System.Data.SqlClient
'Step 1: Select the device ID
Function SelectDevice(d As String, b As String, m As String, o As String) As String
' Try
'Connect to database
Dim xcitoDBConnection As SqlConnection
xcitoDBConnection = New SqlConnection("Data Source=NOTDISPLAYED;" + "Initial Catalog=NOTDISPLAYED;" + "Integrated Security=True")
Dim GetDeviceID As String = "SELECT DISTINCT(Identifier) FROM Support.Devices WHERE DeviceType='" & d & "' AND Brand = '" & b & "' AND Model = '" & m & "' AND OS = '" & o & "'"
Dim command As SqlCommand = New SqlCommand(GetDeviceID, xcitoDBConnection)
xcitoDBConnection.Open()
'command.ExecuteScalar()
Dim reader As SqlDataReader
reader = command.ExecuteReader(CommandBehavior.CloseConnection)
Dim GetID As String
While reader.Read()
GetID = reader("Identifier").ToString
Return GetID
End While
xcitoDBConnection.Close()
' Catch ex As ApplicationException
' ex.Source = ("Selection Error")
' Throw ex
' Finally
'End Try
End Function
End Class
写
reader = command.ExecuteReader()
而不是
reader = command.ExecuteReader(CommandBehavior.CloseConnection)
因为您在 return 之前关闭了连接。 并且 return 函数结束
关注这个
Public Sub CreateCommand(ByVal queryString As String, _
ByVal connectionString As String)
Dim GetID As String
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As New SqlCommand(queryString, connection)
Dim reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
GetID = reader(0).ToString
End While
End Using
Return GetID
End Sub