Click here to Skip to main content
15,883,606 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Probable leak while using SqlDataReader in ADO.NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 Apr 2011CPOL 12.5K   2   2
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:

C#
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());
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
The first ever language I started with was QuickBasic and soon I switched to Borland C++ 3.0. Then I attended computer science school where I learned Cobol, Access and ASP. Now I use VB6, VB.NET, C#, ASP.NET, PHP languages with MSSQL and MySQL databases.

Comments and Discussions

 
GeneralIsn't that basically what the OP said? Pin
AspDotNetDev14-Apr-11 12:32
protectorAspDotNetDev14-Apr-11 12:32 
GeneralRe: Yes for the connection but I'm using it with the SqlCommand ... Pin
Sabrina Hope15-Apr-11 3:39
Sabrina Hope15-Apr-11 3:39 
Yes for the connection but I'm using it with the SqlCommand and SqlDataReader too.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.