65.9K
CodeProject is changing. Read more.
Home

Probable leak while using SqlDataReader in ADO.NET

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Apr 14, 2011

CPOL
viewsIcon

13005

If you want to be sure that every object is closed, I would use this method: while (true){ Thread.Sleep(2000); using (SqlConnection objConnection = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=.;Max Pool Size=1"))...

If you want to be sure that every object is closed, I would use this method:
while (true)
{
    Thread.Sleep(2000);
    using (SqlConnection objConnection = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=.;Max Pool Size=1"))
    using (SqlCommand objCommand = new SqlCommand("Select * from customers", objConnection))
    {
        objConnection.Open();

        using (SqlDataReader reader = objCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
        {
            while (reader.Read())
            {
            }
            Console.WriteLine((i++).ToString());
        }
    }
}