do we need using for the SqlCommand or is it enough just for the SqlConnection and SqlDataReader

前端 未结 2 1349
谎友^
谎友^ 2020-12-19 10:46

i took this code from msdn

string connString = \"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;\";

    using (SqlConnection conn          


        
相关标签:
2条回答
  • 2020-12-19 11:40

    You need a using for every object you create that implements IDisposable. That includes the SqlCommand and the SqlConnection.


    There are very few exceptions to this rule. The main exception is WCF client proxies. Due to a design flaw, their Dispose method can sometimes throw an exception. If you used the proxy in a using statement, this second exception would cause you to lose the original exception.

    0 讨论(0)
  • 2020-12-19 11:53

    You don't NEED to use a using statement, but it is good practice and you SHOULD use it. It allows objects using IDisposable to be disposed of automatically.

    http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx

    Edited to add link and remove inaccurate statement because @John Saunders is right.

    0 讨论(0)
提交回复
热议问题