[英]SqlConnection can't be opened within a 'using' clause of another 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
}
}
}
有什么想法吗? 没有抛出异常。
没有引发异常,所以没有错误...我怀疑连接的隔离级别
在第一种情况下,连接不会重叠。
ExpenseCycle()使用连接字符串,我可以放心地假设它开始一个新的连接。
在第二个示例(问题情况)中,连接确实重叠:
例如,如果隔离级别是已读提交,并且“封闭”连接尚未稳定其写入(提交),则新连接不会选择更改,在这种情况下为插入。
可能的解决方案或要尝试的方法:1.检查连接上的隔离级别2.将连接而不是连接字符串传递给ExpenseCycle()(这是更好的做法,恕我直言)
您可能正在执行一个环境事务(如果在事务范围内调用了代码块,则新连接将自动加入该事务。使用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);
}
我认为第一步应该解决您的问题。 如果不是,第二个绝对应该。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.