-1
我懷疑在相同的代碼塊中以不同的方式應用「使用」語句會產生什麼顯着差異,這將是很好的知道實踐最好的方法爲我 sameple 1碼塊在「使用」語句內啓動sql對象和使用語句裝飾之間的區別
using (SqlConnection SqlConnection = new SqlConnection(dbConnectionString))
{
SqlConnection.Open();
using (var command = new SqlCommand(store_procName, SqlConnection))
{
command.Parameters.Add(Constants.PARAM_Value, SqlDbType.VarChar).Value = Id;
command.CommandType = CommandType.StoredProcedure;
using (var adp = new SqlDataAdapter(command))
{
adp.Fill(dtValid);
}
}
}
return dtValid;
示例代碼塊2
using (SqlConnection SqlConnection = new SqlConnection(dbConnectionString))
{
SqlConnection.Open();
SqlCommand command = new SqlCommand(store_procName, SqlConnection);
command.Parameters.Add(Constants.PARAM_Value, SqlDbType.VarChar).Value = Id;
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(command);
adp.Fill(dtValid);
}
return dtValid;
這不是'asp.net-mv'c特定或'sql-serve'r特定問題。只是c#。另外,請在發佈前花費額外10秒的時間來格式化代碼以提高可讀性。 :) – Shyju
其實我在MVC框架工作認爲有些人可能會遇到這種情況 –
看看http://stackoverflow.com/questions/3715126/what-is-meant-by-connection-dispose-in-c –