Is it necessary to dispose SqlConnection as well as SqlCommand?

后端 未结 2 1186
栀梦
栀梦 2021-01-24 22:49

Is it necessary to separately dispose of a SqlConnection as well as a SqlCommand?

I\'m doing some maintenance work on an application and found the following code:

<
2条回答
  •  轮回少年
    2021-01-24 23:16

    yes , it is necessary to dispose the sqlconnection and sqlcommand object after your piece of code gets executed.

    Now, there are two ways by which you can achieve this:

    1. Write your code in using statement which will take care of disposing the objects, as sqlconnection and sqlcommand implements the IDisposable interface which takes care of disposing the objects..
    2. by calling .close() method explicitly on sqlconnection object.

提交回复
热议问题