2013-07-19 52 views
0

我在我的控制器收到此错误已经有一个与此连接关联的打开DataReader,必须先关闭它。 C#

System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first. 
    at MySql.Data.MySqlClient.MySqlCommand.CheckState() 
    at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) 
    at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) 
    at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) 
    at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) 
    --- End of inner exception stack trace --- 
    at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) 
    at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) 
    at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) 
    at System.Data.Objects.ObjectQuery`1.Execute(MergeOption mergeOption) 
    at System.Data.Objects.DataClasses.EntityReference`1.Load(MergeOption mergeOption) 
    at System.Data.Objects.DataClasses.RelatedEnd.Load() 
    at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad() 
    at System.Data.Objects.DataClasses.EntityReference`1.get_Value() 
    at Timee.fingerprint.get_employee() in C:\Users\MyNameDesktop\Time\Timee\AModel.Designer.cs:line 2234 
    at Timee.BundyForm.verificationControl_OnComplete(Object Control, FeatureSet FeatureSet, EventHandlerStatus& EventHandlerStatus) in C:\Users\MyName\Desktop\Time  \Timee\BundyForm.cs:line 82 
    at DPFP.Gui.Verification.VerificationControl.<>c__DisplayClass2.<relay_OnComplete>b__0() 

控制器:

void verificationControl_OnComplete(object Control, DPFP.FeatureSet FeatureSet, ref DPFP.Gui.EventHandlerStatus EventHandlerStatus) 
    { 
     clearInfoBoxTimer.Stop(); 

     DateTime entryTime = DateTime.Now; 

     DPFP.Verification.Verification ver = new DPFP.Verification.Verification(); 
     DPFP.Verification.Verification.Result res = new DPFP.Verification.Verification.Result(); 

     employee employees = null; 
     foreach (fingerprint fingerPrint in this.db.fingerprints) 
     { 
      DPFP.Template template = new DPFP.Template(); 
      template.DeSerialize(fingerPrint.data); 
      ver.Verify(FeatureSet, template, ref res); 
      if (res.Verified) 
      { 
       employees = fingerPrint.employee; //Im GETTING AN ERROR HERE 
       break; 
      } 
     } 
    } 

基于一些论坛上我读我要补充multipleactiveresultsets=True;我的webconfig。但在我的情况下,它不适用,因为我使用不支持它的MYSQL。有没有其他方法可以做到这一点?请帮帮我,谢谢。验证

// Summary: 
//  Performs the system function of fingerprint verification, which is a one-to-one 
//  comparison of a fingerprint feature set with a fingerprint template produced 
//  at enrollment that returns a decision of match or non-match. 
public class Verification 
{ 
    // Summary: 
    //  Use this value to specify the default FAR threshold 
    public const int ProbabilityNotSet = -1; 

    // Summary: 
    //  Initializes a new instance of the Verification class for comparing a fingerprint 
    //  feature set with a fingerprint template using the default value of the false 
    //  accept rate (FAR) 
    public Verification(); 
    // 
    // Summary: 
    //  Initializes a new instance of the Verification class for comparing a fingerprint 
    //  feature set with a fingerprint template and assigns the value of the FAR 
    // 
    // Parameters: 
    // FARRequested: 
    //  Value of the requested FAR 
    public Verification(int FARRequested); 

    // Summary: 
    //  Returns or assigns the requested false accept rate (FAR) 
    public int FARRequested { get; set; } 

    // Summary: 
    //  Performs fingerprint verification and returns the comparison decision based 
    //  on the default FAR threshold 
    // 
    // Parameters: 
    // FeatureSet: 
    //  A DPFP.FeatureSet object 
    // 
    // Template: 
    //  A DPFP.Template object 
    // 
    // Returns: 
    //  Verification result object 
    public static Verification.Result Verify(FeatureSet FeatureSet, Template Template); 
    // 
    // Summary: 
    //  Performs fingerprint verification and returns the comparison decision based 
    //  on the specified FAR threshold 
    // 
    // Parameters: 
    // FeatureSet: 
    //  A DPFP.FeatureSet object 
    // 
    // Template: 
    //  A DPFP.Template object 
    // 
    // FARRequested: 
    //  False Accept probability threshold or ProbabilityNotSet to use the default 
    //  threshold 
    // 
    // Returns: 
    //  Verification result object 
    public static Verification.Result Verify(FeatureSet FeatureSet, Template Template, int FARRequested); 
    // 
    // Summary: 
    //  Performs the system function of fingerprint verification and specifies a 
    //  comparison decision based on the FAR set by the FARRequested property 
    // 
    // Parameters: 
    // FeatureSet: 
    //  A DPFP.FeatureSet object 
    // 
    // Template: 
    //  A DPFP.Template object 
    // 
    // Result: 
    //  A DPFP.Verification.Result object 
    public void Verify(FeatureSet FeatureSet, Template Template, ref Verification.Result Result); 

    // Summary: 
    //  Represents the results of a fingerprint verification operation. 
    public class Result 
    { 
     // Summary: 
     //  Default c-tor 
     public Result(); 

     // Summary: 
     //  Returns or assigns the value of the achieved FAR for a comparison operation. 
     public int FARAchieved { get; set; } 
     // 
     // Summary: 
     //  Returns or assigns the comparison decision, which indicates whether the comparison 
     //  of a fingerprint feature set and a fingerprint template resulted in a decision 
     //  of match or non-match. This decision is based on the value of the FARRequested 
     //  property 
     public bool Verified { get; set; } 
    } 
} 

回答

0

我现在能够找到我自己的问题的答案。这是我工作的解决方案

void verificationControl_OnComplete(object Control, DPFP.FeatureSet FeatureSet, ref DPFP.Gui.EventHandlerStatus EventHandlerStatus) 
    { 
     clearInfoBoxTimer.Stop(); 

     DateTime entryTime = DateTime.Now; 

     DPFP.Verification.Verification ver = new DPFP.Verification.Verification(); 
     DPFP.Verification.Verification.Result res = new DPFP.Verification.Verification.Result(); 

     employee employees = null; 
     foreach (fingerprint fingerPrint in this.db.fingerprints) 
     { 
      DPFP.Template template = new DPFP.Template(); 
      template.DeSerialize(fingerPrint.data); 
      ver.Verify(FeatureSet, template, ref res); 
      if (res.Verified) 
      { 
       db.Connection.Close(); //I close the connection first 
       db.Connection.Open(); // then I open it again 

       employees = fingerPrint.employee; 
       break; 
      } 
     } 
    } 

基于这里的人们和一些论坛,我读的建议,我需要先关闭打开的阅读器,然后再次打开它。我花了时间思考如何做到这一点,最后我现在能够找到一个。这就是我如何以最简单的方式解决我的问题,我不知道是否有其他解决方案。但至少这解决了我的问题,我仍然非常感谢这里的人们尽力帮助我。感谢你们。 :)

1

模型并在下面的代码帮助?

foreach (fingerprint fingerPrint in this.db.fingerprints) 
{ 
    using(fingerprint) 
    { 
     DPFP.Template template = new DPFP.Template(); 
     template.DeSerialize(fingerPrint.data); 
     ver.Verify(FeatureSet, template, ref res); 
     if (res.Verified) 
     { 
      employees = fingerPrint.employee; //Im GETTING AN ERROR HERE 
      break; 
     } 
    } 
} 
+0

我在这条线“使用(指纹)”在使用语句中使用类型得到一个错误,必须是隐式转换为System.IDisposable的。 – bot

+0

什么是db对象...我推测潜在的将是EF –

+0

是的db对象是EF – bot

1

您无法在数据读取器内部打开数据读取器。 (当已经有打开的阅读器时,通常以嵌套循环的方式打开阅读器)。

作为一家公司,过去我们遇到过这个问题。经过大量调查,我们决定,为了我们的目的,重新思考我们如何构建数据连接。

您首先必须关闭打开的阅读器,然后打开下一个阅读器。因此,我们继续编写更多面向对象的代码,使用类对象作为顶级循环的“存储”,并使用我们需要的数据填充该对象。

关闭阅读器。

逐步通过临时对象并打开读取器以获取更多数据。

清洗,冲洗,重复。

这对我们来说一直很好。

P.S,您还可以通过更好地使用表连接来消除嵌套的读循环。

+0

感谢您的宝贵信息。我会尽量考虑一下。我必须与我的团队讨论这个问题。 – bot

1

什么样的数据类型是res?什么样的数据类型是指纹?

您可能需要为结果提供更多上下文,但听起来应该返回数据集/数据适配器而不是返回ADO.NET连接。 fingerPrint.employee可能使用相同的底层连接,但是您尚未关闭第一个阅读器,因此会显示错误消息。

如果您实施此策略,它将允许您从底层数据库实例中分离出来。

因此,您的数据库层将在内部使用IDataReader,然后返回上层可以迭代的DataSet或DataAdapter,而不必担心打开的数据库连接。

+0

我使用BLOB作为指纹的数据类型。对于我不能说的只是看看我的模型,我把它列入我的问题。 – bot

0

当您使用foreach遍历指纹时,它保持连接打开。一种解决方案是在指纹上调用ToList(),指纹将获取所有指纹并将它们放入列表中,然后关闭连接。然后,您可以使用foreach遍历列表,并为列表中的每个项目执行附加查询。例如:

foreach (fingerprint fingerPrint in this.db.fingerprints.ToList()) 

这个问题有一些其他的答案:

Entity Framework: There is already an open DataReader associated with this Command

相关问题