tags:

views:

14

answers:

1

Hi All,

In the below code,

 using (SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (dr.Read())
                    {
                        _emailTemplate.EmailContent = dr["EMAILCONTENT"].ToString();
                        _emailTemplate.From = dr["EMAILFROM"].ToString();
                        _emailTemplate.Subject = dr["EMAILSUBJECT"].ToString();
                    }
                }

I understand CommandBehavior.CloseConnection will close the connection object when datareader is closed. In the above code, when we use using with SqlDataReader, will it close the datareader before disposing it? in other other words, if i use using statement do i need to close the datareader manually?

+1  A: 

No, you will not need to close the reader manually. At the end of a 'using' block, .NET calls the Dispose() method on the item if it supports the IDispoable interface. The SQLDataReader does support this and closes itself with it's dispose method is called. Meaning that at the of the using block, dispose is called which closes the dataReader for you.

Tommy

related questions