2

我很好奇这是为什么。我今天早些时候遇到了这种情况

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
        }
    }
}

任何想法为什么?没有抛出异常。

4

2 回答 2

3

没有抛出异常,所以没有错误......我怀疑连接上的隔离级别

在第一种情况下,连接不重叠。

ExpenseCycle() 使用连接字符串,我可以放心地假设它启动了一个新连接。

在第二个示例(问题案例)中,连接确实重叠:

例如,如果隔离级别是已提交读,并且“封闭”连接尚未稳定其写入(提交),则新连接不会接收更改,在这种情况下是插入。

可能的解决方案或要尝试的事情: 1. 检查连接上的隔离级别 2. 将连接而不是连接字符串传递给 ExpenseCycle() (这也是一个更好的做法,恕我直言)

于 2012-10-12T13:05:13.337 回答
1

您可能有一个生效的环境事务(如果在事务范围内调用代码块,新连接将自动加入该事务。使用TransactionScope 类,您可以获得该事务的句柄并在第二次调用之前提交它.

此外,您的第二次调用似乎在命令的 using 块范围内。将其移出可能足以解决您的问题

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;
      }
   }//close the SqlCommand
   //Get the new set of ExpenseCycles for binding
   ExpenseCycle cycle = new ExpenseCycle(ConnectionString);
   return cycle.GetExpenseCycles(portalId);
   //This might fix your problem. 
} 

另一种选择是将第二个调用移到第一个 using 块之外,就像这样

bool insertSuccessful;
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();
         insertSuccessful=true;
      }
      catch (SqlException ex)
      {
         insertSuccessful=false
         throw ex;
      }
   }//close the SqlCommand
}//close the connection
//Get the new set of ExpenseCycles for binding
if(insertSuccessful)
{
   ExpenseCycle cycle = new ExpenseCycle(ConnectionString);
   return cycle.GetExpenseCycles(portalId);
}

我认为第一个块应该可以解决您的问题。如果不是第二个肯定应该。

于 2012-10-12T13:27:24.260 回答