【发布时间】:2013-11-10 22:51:16
【问题描述】:
好的,这是微不足道的,但它一直困扰着我。我有这样的代码
oCmd = new SqlCommand("getBooking", oCon);
oCmd.CommandType = CommandType.StoredProcedure;
oRs = oCmd.ExecuteReader();
do while (oRs.Read()) {
// do stuff
}
oRs.Close();
oCmd.Dispose();
但是我可以像这样将Dispose 移动到ExecuteReader 之后吗:
oCmd = new SqlCommand("getBooking", oCon);
oCmd.CommandType = CommandType.StoredProcedure;
oRs = oCmd.ExecuteReader();
oCmd.Dispose();
do while (oRs.Read()) {
// do stuff
}
oRs.Close();
我试过了,它有效,但感觉我很顽皮。这种方法有问题吗?我问是因为经常需要在do while 中重复使用SqlCommand 对象,而且我不想创建多个SqlCommand 对象。
【问题讨论】:
-
如果您打算在循环中再次使用
SqlCommand,请不要在循环之前处理它,也许使用using语句会使一切变得更清晰
标签: c# asp.net dispose sqlcommand