Click here to Skip to main content
11,495,049 members (55,098 online)
Click here to Skip to main content

Tagged as

Probable leak while using SqlDataReader in ADO.NET

, 14 Apr 2011 CPOL 4.2K 2
Rate this:
Please Sign up or sign in to vote.
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());
        }
    }
}

License

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

Share

About the Author

DizZ
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 13:32
protectorAspDotNetDev14-Apr-11 13:32 
GeneralRe: Yes for the connection but I'm using it with the SqlCommand ... Pin
DizZ15-Apr-11 4:39
memberDizZ15-Apr-11 4:39 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 14 Apr 2011
Article Copyright 2011 by DizZ
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid