Does End Using close an open SQL Connection

前端 未结 6 1505
礼貌的吻别
礼貌的吻别 2020-11-29 05:53

If I wrap a SQLConnection in a Using, should I close it or does the end using handle it?

using cn as new system.data.sqlclient.sqlconnection()
    cn.open
           


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 06:07

    using is just a shorthand to try/finally. this is equivilent code to what you posted

    Try
        SqlConnection cn as new system.data.sqlclient.sqlconnection()
        cn.open
        '{do a bunch of other stuff with commands and datareaders here}
        cn.close 'Do I need this?
    Finally
        cn.Dispose()
    End Try
    

    Dispose is supposed to take care of all resource cleanup, in the case of connections it will close it.

提交回复
热议问题