我能把using()语句只放在SqlConnection上吗?它还会释放子进程SqlCommand和SqlDataReader吗?我可以这样做吗: using(SqlConnection connection = new SqlConnection("connection string"))
SqlCommand cm
使用这种方法有什么好处吗?using (SqlConnection conn = new SqlConnection(conStr)) using (SqlCommand command = new SqlCommandSqlCommand command = new SqlCommand();}
显然,如果您计划使用同一连接运行多个命令,这一点很重要,因为关闭SqlDataR
SqlConnection、SqlCommand和SqlDataReader都实现了IDisposable接口。我读到过一个关于总是将IDisposables包装到using块中的最佳实践。因此,我查询数据的常见场景是这样的(当然,在更广泛的上下文中,像linq2sql这样的映射工具是合适的,但假设我们想在这里使用这种方法):
using (SqlConnection cn = new SqlConnection("myConnections