2017-07-16 52 views

回答

1

究竟是什么让你感到不满意?在代码中换行是可以的,除了可以将它放入using语句以确保即使发生错误时也能处理它,异常:

using (var sqlconn = new ...) 
using (var sqlcom = new ...) 
{ 
    sqlcon.open(); 
    sqlcom.ExecuteNonQuery(); 
} 

这样Dispose(其会自动调用Close)被称为离开using时,无论是在通常的方式或通过一个例外。

2

using会照顾它。在引擎盖下,SqlConnection.Dispose()调用SqlConnection.Close()方法,并且SqlCommand.Dispose()调用SqlCommand.Close()

作为附加背景,using statementtry ... finally的语法糖,它将IDisposable对象置于finally中。

相关问题