好的,如果我将 dbDataReader 放在“Using”语句中,我是否仍需要显式调用 dbDataReader.Close。在 .net 文档中,它说“使用”连接会自动关闭它。在下面来自 MSDN 的示例中,它显示了一个没有“Using”并显式关闭它的 dbDataReader:
Public Sub ReadData(ByVal connectionString As String)
Dim queryString As String = _
"SELECT OrderID, CustomerID FROM Orders"
Using connection As New OdbcConnection(connectionString)
Dim command As New OdbcCommand(queryString, connection)
connection.Open()
Dim reader As OdbcDataReader
reader = command.ExecuteReader()
' Always call Read before accessing data.
While reader.Read()
Console.WriteLine(reader.GetInt32(0) & ", " & reader.GetString(1))
End While
' Always call Close when done reading.
reader.Close()
End Using
End Sub
所以这不是更清洁,更高效:
Public Sub ReadData(ByVal connectionString As String)
Dim queryString As String = _
"SELECT OrderID, CustomerID FROM Orders"
Using connection As New OdbcConnection(connectionString)
Using command As New OdbcCommand(queryString, connection)
connection.Open()
Using reader = command.ExecuteReader()
' Always call Read before accessing data.
While reader.Read()
Console.WriteLine(reader.GetInt32(0) & ", " & reader.GetString(1))
End While
End Using
End Using
End Using
End Sub
然后你就不需要在阅读器上显式调用 .Close 了吗?
提前致谢