Using statement question

后端 未结 6 1023
挽巷
挽巷 2021-01-13 04:03

I have two questions.

1) Should you always use a using statement on a connection? So, I would use it on the connection and then another one on a reader within the c

6条回答
  •  耶瑟儿~
    2021-01-13 04:52

    Special point on 1). You need to specifically avoid that technique when the connection is used in asynchronous ADO.NET methods - like BeginExecuteReader, because more than likely, you will fall out of scope and try to dispose the connection while the async operation is still in progress. This is similar to the case when you are using class variables and not local variables. Often times the connection reference is stored in a class used as the "control block" for the asynchronous operation.

提交回复
热议问题