Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
.Dispose
Message
From
21/03/2006 20:45:32
 
 
To
21/03/2006 16:02:15
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Miscellaneous
Thread ID:
01106252
Message ID:
01106470
Views:
19
SQLConnection is managed, so it's not necessary. But to write code that doesn't depend on your connection type, the following is the recommendation from Rocky:
So, follow this basic pattern in VB.NET 2002/2003 (pseudo-code):

 

Dim cn As New Connection("…")

cn.Open()

Try

  Dim cm As Command = cn.CreateCommand()

  Try

    Dim dr As DataReader = cm.ExecuteReader()

    Try

      ' do data reading here

 

    Finally

      dr.Close() ' and/or Dispose() – though Close() and Dispose() both work

    End Try

 

  Finally

    cm.Dispose()

  End Try

 

Finally

  cn.Close() ' and/or Dispose() – though Close() and Dispose() both work

End Try

 




And in C# 1.0 and 2.0 (pseudo-code):

 

using(Connection cn = new Connection("…"))

{

  cn.Open()

  using(Command cm = cn.CreateCommand())

  {

    using(DataReader dr = cm.ExecuteReader())

    {

      // do data reading here

    }

  }

}

 

And in VB 8 (VB.NET 2005) (pseudo-code):

 

Using cn As New Connection("…")

  cn.Open()

  Using cm As Command = cn.CreateCommand()

    Using dr As DataReader = cm.ExecuteReader()

      ' do data reading here

    End Using

  End Using

End Using
>I'm not sure this is always the case. With ODBC or OLE DB access, there will be unmanaged code called, but if you are using a .Net Provider, I think it's all managed code.
>
>>I just googled and the first page listed has a good example:
>>
>>http://davidhayden.com/blog/dave/archive/2005/01/13/773.aspx
>>
>>In the C# class I took last quarter, we had an assignment to use GDI+ to print. I found some good examples to follow. The one I used emphasized to dispose of the fonts, brushes, etc. you created for the printing since it was unmanaged.

(On an infant's shirt): Already smarter than Bush
Previous
Reply
Map
View

Click here to load this message in the networking platform