我从Linq查询中收到以下错误:
ExecuteReader需要一个开放且可用的连接。该 连接的当前状态是打开的。
如果需要打开连接,并且连接已打开...为什么这是一个错误?
关于此错误的堆栈溢出是post,解决方案是:
“基本上,我们的应用程序中有一些'流氓查询' 正在影响整个工作正常,所以我们只是 通过每个班级(所有88个!)并检查他们是谁 写得不错。“
我试图查询我的查询问题而不能。我测试了它,它得到了正确的结果..
这是我的疑问:
int? result = Helper.GetResult(b).FirstOrDefault();
if (result.HasValue)
//something
-----------------------------------------------------------
Inside Helper:
private static PingDbDataContext pingDataContext = new PingDbDataContext();
public static IQueryable<int?> GetResult(string b)
{
pingDataContext.CommandTimeout = 5000;
pingDataContext.Log = new DebugTextWriter();
var q = from a in pingDataContext.SomeTable
where a.SomeColumn == b
select a.id;
return q;
}
这是堆栈跟踪:
Exception information:
Exception type: InvalidOperationException
Exception message: ExecuteReader requires an open and available Connection. The connection's current state is open.
at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
at MyProject.Controllers.MyController.Index() in (path...)\MyProject\Controllers\MyController.cs:line 53
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
答案 0 :(得分:1)
由于这是一个MVC项目,并且您的数据上下文是一个静态变量,因此很可能是一个并发问题,就像BrokenGlass建议的那样。可以肯定的是,尝试在方法中创建数据上下文,如下所示:
public static IEnumerable<int?> GetResult(string b)
{
using (PingDbDataContext pingDataContext = new PingDbDataContext())
{
pingDataContext.CommandTimeout = 5000;
pingDataContext.Log = new DebugTextWriter();
var q = from a in pingDataContext.SomeTable
where a.SomeColumn == b
select a.id;
return q.ToList();
}
}
问题是否仍然存在?
编辑:我已经重写了上述方法。您不应该在数据上下文范围之外返回IQueryable。在处理数据之前,您需要从数据库中检索数据。但是,无论如何,我都不会想到将IQueryable暴露在您的数据访问代码之外。如果您想修改在方法之外运行的查询,则只需要这样做。