1

我的 Linq 查询出现以下错误:

ExecuteReader 需要一个打开且可用的连接。连接的当前状态是打开的。

如果它需要一个打开的连接,并且连接是打开的......为什么这是一个错误?

这是有关此错误的 stackoverflow 帖子,解决方案是:

“基本上,我们的应用程序中有一些‘流氓查询’影响了整个事情的正常工作,所以我们只需要检查每一节课(全部 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)
4

1 回答 1

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 并不是一个好主意。仅当您想修改在您的方法之外运行的查询时才需要它。

于 2012-06-21T04:47:57.153 回答