Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Design Question
Message
From
21/03/2009 12:24:29
 
 
General information
Forum:
ASP.NET
Category:
ADO.NET
Title:
Miscellaneous
Thread ID:
01389713
Message ID:
01390261
Views:
70
You're right Viv (and Naomi and Chris). It's the implicitly invoked Dispose() that closes the connection. My bad! I was probably just thinking about it going out of scope.

~~Bonnie


>>>The SqlConnection will close when it exits the using clause.
>>
>>No, I don't think so ... I'm pretty sure that even though the oConn object goes out of scope, the connection will remain open.
>>
>I'm believe it *does* close the connection. After all it's not just going out of scope - it's also implicitly invoking Dispose()
>Best,
>Viv
>
>
>>
>>
>>>>>>>In other words, if you use this syntax, the connection object is automatically closed.
>>>>>>
>>>>>>Ok, I see. But I'm not sure how to use it in my class. Can you elaborate a bit?
>>>>>
>>>>>I don't know if this is applicable in your class and I'm not 100% sure the syntax will work as is.
>>>>>
>>>>>Can you post a bit of code how you're using this method in your code right now and we'll see, if my idea can work for you or not.
>>>>
>>>>
>>>>Sure. It's a bit long tho...
>>>>
>>>
>>>Here's your code, with the using statement:
>>>
>>>
>>>public SqlDataReader ExecuteReader(string sCommand, ArrayList colParams, CommandType Type)
>>>{
>>>
>>>    SqlDataReader oRetVal = null;
>>>
>>>    using (SqlConnection oConn = _GetConnection(true))
>>>    {
>>>        if (oException == null)
>>>        {
>>>            SqlCommand oCommand = new SqlCommand();
>>>
>>>            oCommand.Connection = oConn;
>>>            oCommand.CommandText = sCommand;
>>>
>>>            if (colParams != null)
>>>            {
>>>                SqlParameter[] aParams = new SqlParameter[colParams.Count];
>>>                colParams.CopyTo(aParams);
>>>
>>>                oCommand.Parameters.Clear();
>>>                oCommand.Parameters.AddRange(aParams);
>>>            }
>>>
>>>            oCommand.CommandType = Type;
>>>
>>>            try
>>>            {
>>>                oRetVal = oCommand.ExecuteReader();
>>>            }
>>>            catch (SqlException e)
>>>            {
>>>                _oException = e;
>>>            }
>>>
>>>            if (_oException == null)
>>>            {
>>>                oCommand.Parameters.Clear();
>>>            }
>>>        }
>>>
>>>        return oRetVal;
>>>    }
>>>}
>>>
>>>
>>>The SqlConnection will close when it exits the using clause.
>>>
>>>Still, even with your code, I would think that you should be able to use oConn.Close() at the end of ExecuteReader method, though I haven't tried it.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform