이유가 궁금합니다. 나는 오늘 아침다른 SqlConnection의 'using'절에서 SqlConnection을 열 수 없습니까?
using (SqlConnection oConn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("IC_Expense_InsertCycle", oConn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PortalId", portalId);
cmd.Parameters.AddWithValue("@Description", description);
cmd.Parameters.AddWithValue("@StartDate", start);
cmd.Parameters.AddWithValue("@EndDate", end);
try
{
oConn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw ex;
}
}
}
//Get the new set of ExpenseCycles for binding
ExpenseCycle cycle = new ExpenseCycle(ConnectionString);
return cycle.GetExpenseCycles(portalId);
// ^^ this works just fine. The GetExpenseCycles call will basically set up the structure above with using SqlConnection and using SqlCommand
using (SqlConnection oConn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("IC_Expense_InsertCycle", oConn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PortalId", portalId);
cmd.Parameters.AddWithValue("@Description", description);
cmd.Parameters.AddWithValue("@StartDate", start);
cmd.Parameters.AddWithValue("@EndDate", end);
try
{
oConn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw ex;
}
//Get the new set of ExpenseCycles for binding
ExpenseCycle cycle = new ExpenseCycle(ConnectionString);
return cycle.GetExpenseCycles(portalId);
//This didn't work. The INSERT statement was successful, but it was bringing back old entries, and did not include the newest one that was just inserted
}
}
하단 코드 블록은 내가 가진 것을 처음이었다이 시나리오에 달리고, 내 테스트 환경에 대한 반환 카운트는 단 1을했지만, 데이터베이스에이 개 기록이 있었다. 새로 삽입 된 레코드를 가져 오지 않았습니다.
using (SqlConnection oConn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("IC_Expense_GetExpenseCyclesByPortal",oConn))
{
oConn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
//Read List<expensecycle> here
}
}
}
어떤 아이디어 이유 :
GetExpenseCycles의 기본 코드는 다음과 같다? 예외가 발생하지 않았습니다. 던져
Hrrrm, 일반적인 throw로 인해'SqlException'이 아닌 다른 것을 던질 것이라고 생각하십니까? – dgarbacz
'SqlException'으로 무엇인가를하려고하지 않는다면, 그냥 다시 던지기 만하면됩니다. – James
코드 내에 return 문이 있으므로 첫 번째 삽입 만 실행됩니다. –