无法在另一个SqlConnection的'using'子句中打开SqlConnection?

时间:2012-10-12 12:49:23

标签: c# asp.net 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,但数据库中有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
        }
    }
}

任何想法为什么?没有例外。

2 个答案:

答案 0 :(得分:3)

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

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

ExpenseCycle()使用连接字符串,我可以安全地假设它开始一个新的连接。

在第二个示例(问题情况)中,连接重叠:

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

可能的解决方案或尝试的事项: 1.检查连接上的隔离级别 2.将连接而不是连接字符串传递给ExpenseCycle()(这是一种更好的做法)

答案 1 :(得分:1)

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

此外,您的第二次调用似乎在命令的使用块范围内。将它移到那里可能足以解决你的问题

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

另一种选择是将第二个调用移出第一个使用块之外,如此

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

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

相关问题