With dbDataReaders and "Using" do you still need to call Close?

155 views Asked by At

Ok, If I put a dbDataReader in a "Using" statement do I still need to explicitly call the dbDataReader.Close. In the .net documentation it says that "Using" a connection automatically Closes it. In the example below from MSDN it shows a dbDataReader without a "Using" and explicitly closing it:

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

So isn't this cleaner and more efficient:

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

And then you wouldn't need to explicitly call the .Close on the Reader?

Thanks in advance

3

There are 3 answers

4
Al W On

In the code snippet you are using, you don't need to explicitly call .Close on the reader. However, it's a good habit to get into. Explicitly close the reader and the connection as soon as you are finished using them.

8
paparazzo On

For me best practice is neither
A try, catch, finally to handle any SQL exceptions

SqlConnection sqlConnRW = new SqlConnection(sqlConnStringLibDef);
try
{
    sqlConnRW.Open();
    SqlCommand sqlCmd = sqlConnRW.CreateCommand();
    sqlCmd.CommandText = "select column from table";
    SqlDataReader rdr = sqlCmd.ExecuteReader();
    while (rdr.Read())
    { }
    rdr.Close();
}
catch (SqlException Ex)
{
    Debug.WriteLine(Ex.Message);
    Debug.WriteLine(Ex.Number);
}
finally
{
    sqlConnRW.Close();
}
2
usr On

Close is 100% redundant to Dispose. using calls Dispose in a safe way. Just always put your resources in a using block. It is made for this purpose. It is safe by construction. It is easy to inspect and immediately see that it is correct.

No need to call Close. It is redundant and confusing. Just always follow the using habit if you can.

Resource handling in C# and VB.NET is very easy. Just use using!

If you require error handling, just add a try-catch inside or outside the using.