我很好奇这是为什么。我今天早些时候遇到了这种情况
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,但数据库中有 2 条记录。它没有获取新插入的记录。
GetExpenseCycles 的基本代码如下:
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
}
}
}
任何想法为什么?没有抛出异常。