Note that there are some explanatory texts on larger screens.

plurals
  1. POImportance of closing SQLConnection objects when using the SQLDataReader object
    primarykey
    data
    text
    <p>My present contract engagement is at a large E-Commerce company. Their code base which has origins going back to .Net 1.0 has caught me by surprise to contain many issues that raise the level of smell beyond the last crap I took. </p> <p>That notwithstanding and trying to diffuse my level of distraction from it, I go along merrily trying to add in features to either fix other problems or extend more crap. Where I touch the DAL/BLL the time it will take to fix the aforementioned will be done. However I wanted to get a vote of confidence from the experts to get some assurance of not wasting the clients time or worse having my credibility voted down by touching "stuff that works". Of course unit testing would solve or at least soften this worry. Perhaps this should also be added to the wtf.com?</p> <pre><code>Public Function GetSizeInfoBySite(ByVal siteID As String) As IList Dim strSQL As String = "YES INLINE SQL!! :)" Dim ci As CrapInfo Dim alAnArrayList As ArrayList Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) Dim cmd As New SqlCommand(strSQL, cn) cmd.Parameters.Add(New SqlParameter("@MySiteID", SqlDbType.NVarChar, 2)).Value = siteID cn.Open() Dim rs As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) While rs.Read() ci = New CategoryInfo(rs("someID"), rs("someName")) If IsNothing(alAnArrayList) Then alAnArrayList = New ArrayList End If alAnArrayList.Add(ci) End While rs.Close() Return CType(alAnArrayList, IList) End Function </code></pre> <p>Does anyone see problems with this aside from the inline SQL which makes my gut churn? At the least wouldn't you ordinarily wrap the above in a try/catch/finally which most of us knows has been around since .Net v1.0? Even better would'nt it be wise to fix with Using statements? Does the SQLDataReader close really encapsulate the connection close automagically? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload