Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Design Question
Message
De
20/03/2009 04:30:47
 
 
À
20/03/2009 01:07:55
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Titre:
Divers
Thread ID:
01389713
Message ID:
01389899
Vues:
71
>>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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform