MySQL Reader立即关闭

时间:2016-06-06 00:07:19

标签: c# mysql reader

我有一个递归填充列表的方法。首先,它选择父项,然后使用外键选择它可能拥有的任何子项。它选择父项是正确的,但是当它开始挑选子项时,Reader无缘无故地关闭。

我已经在调试器中完成了它,并且我没有理由看到它正在关闭。搜索参数有效,搜索返回项目(我手动检查),Reader处于活动状态。但出于某种原因,一旦打开,它就会关闭,没有任何错误。

  • 连接字符串有效
  • 读者有效
  • 搜索参数有效
  • 该命令返回结果(我专门测试了这个 - WHERE 1 = 1 AND SE.ParentSE = 4;";有超过10个结果)

我不知道发生了什么事?

try
{
    using (MySqlConnection connection = new MySqlConnection(GetConnectionString()))
    {
        connection.Open();

        using (MySqlCommand command = connection.CreateCommand())
        {
            command.CommandText = @"SELECT 
                                    SE.SEId,
                                    SE.Name,
                                    SE.Status,
                                    SE.Description,
                                    SE.ParentME AS ParentMEId,
                                    SE.ParentSE AS ParentSEId,
                                    SE.Comments AS Comments,
                                    SE.Removed AS Removed,
                                    SE.SupplierId AS SupplierId,
                                    TC.TerminalCount AS TerminalCount,
                                    SE2.Name AS ParentSE,
                                    ME.EquipmentNumber AS ParentME,
                                    D.DSId AS DataSheet,
                                    D.DSType AS DataSheetType,
                                    C.Code AS EquipmentType,
                                    C.Description AS TypeDescription,
                                    C.CodeType AS TypeDiscipline,
                                    SE.EquipmentType AS EquipTypeId,
                                    SE3.SEId AS SE3Id,
                                    SE4.SEId AS SE4Id,
                                    SE5.SEId AS SE5Id,
                                    SE6.SEId AS SE6Id,
                                    SE2.ParentME AS ME2Id,
                                    SE3.ParentME AS ME3Id,
                                    SE4.ParentME AS ME4Id,
                                    SE5.ParentME AS ME5Id,
                                    SE6.ParentME AS ME6Id,
                                    PAC.UDCId AS AreaCodeId,
                                    PAC.Code AS AreaCode,
                                    PAC.Description AS AreaCodeDescription
                                FROM SubEquipment SE
                                LEFT  JOIN DataSheet D ON SE.SEId = D.SEId
                                LEFT JOIN (SELECT SEId, COUNT(*) as TerminalCount FROM Terminal GROUP BY SEId) TC ON SE.SEId = TC.SEId
                                LEFT JOIN SubEquipment SE2 ON SE.ParentSE = SE2.SEId
                                LEFT JOIN MajorEquipment ME ON SE.ParentME = ME.MEId
                                LEFT JOIN UserDefinedCode C ON C.UDCId = SE.EquipmentType
                                LEFT JOIN SubEquipment SE3 ON SE2.ParentSE = SE3.SEId
                                LEFT JOIN SubEquipment SE4 ON SE3.ParentSE = SE4.SEId
                                LEFT JOIN SubEquipment SE5 ON SE4.ParentSE = SE5.SEId
                                LEFT JOIN SubEquipment SE6 ON SE5.ParentSE = SE6.SEId
                                INNER JOIN MajorParentage MP ON SE.SEId = MP.SEId
                                INNER JOIN MajorEquipment MEU ON MP.MEId = MEU.MEId
                                INNER JOIN UserDefinedCode PAC ON MEU.PACId = PAC.UDCId
                                LEFT JOIN ComponentStatus CS1 ON CS1.StatusName = SE.Status
                                WHERE 1 = 1"
                                // " AND SE.ParentSE = @SEParentId"                                    
                                // @SEParentId = 4
                                + parentSearch
                                + ";";

            command.Parameters.AddWithValue("@PACId", areaCodeId);
            command.Parameters.AddWithValue("@AreaCode", areaCode.Cipher());
            command.Parameters.AddWithValue("@ETId", equipmentTypeId);
            command.Parameters.AddWithValue("@EquipmentType", equipmentType.Cipher());
            command.Parameters.AddWithValue("@EquipmentNumber", equipmentNumber.Cipher());
            command.Parameters.AddWithValue("@Description", description.Cipher());
            command.Parameters.AddWithValue("@Comments", comments.Cipher());
            command.Parameters.AddWithValue("@TypeDiscipline", typeDiscipline);
            command.Parameters.AddWithValue("@MEParentId", parentMEId);
            command.Parameters.AddWithValue("@MEParent", parentMENumber.Cipher());
            command.Parameters.AddWithValue("@SEParentId", parentSEId);
            command.Parameters.AddWithValue("@SEParent", parentSENumber.Cipher());
            command.Parameters.AddWithValue("@SEId", excludeSEId);
            command.Parameters.AddWithValue("@Status", status.Cipher());

            using (MySqlDataReader reader = command.ExecuteReader())
            {
                // Read all the results and populate a model with them.
                while (reader.Read())
                {
                    // Do stuff...
                }
                // Finished reading the results
                if (!reader.IsClosed)
                    reader.Close();
            }
        }

        // Finished with the connection
        if (connection.State == System.Data.ConnectionState.Open)
            connection.Close();
    }
}
catch (MySqlException e)
{
    ShowMySQLError(e, 4417);
}

1 个答案:

答案 0 :(得分:0)

找到它。

该方法在整个计划的多个地方使用;但由于某种原因,只有调用意外关闭了Reader。仔细观察 - 读者的属性显示它没有行:HasRows = false。意味着查询有问题。

我再次看了一眼,发现这个问题是一个被忽视的参数(我为了这个问题而修剪了这个问题 - 我并不认为这是必要的)。有一些默认的boolean参数,其中一个设置为true,导致查询返回空结果。

故事的道德:了解您的代码正在做什么!