即使所有代码都正确,我的布尔值也为false

时间:2017-07-04 09:21:27

标签: c# mysql

关于我的计划:

我的算法(此类)用于检查交付是否已完成,之后使卡车/拖车/驱动程序可用于另一次交付,同时此算法在交付时发送另一辆卡车/拖车/驱动程序。总结一下,这个课程做了以下几点:

  • 检查预订是否处于分配模式(分配模式基本上意味着"预订"正在交付
  • 检查预订是否还有吨位(例如我想将500吨送到一个地方,但一次只能送30吨,所以检查是否还有吨需要送货)
  • 使已完成交付的司机/卡车/拖车可用。
  • 自动为驾驶员/拖车/卡车分配吨位。
  • 删除已完成的条目(任何已完成的预订 - 日志仍保留在数据库中)。

我的问题:

我不知道我的班级有什么问题,我已经花了好几个小时,似乎无法找出导致我的布尔变量(forLoopBreak)触发" false"调用checkAvailTrailers()方法时的值。问题似乎在那里,但我无法弄清楚导致问题的原因。

类别:

[https://pastebin.com/4up8eppd][1] 

(当我遇到字符限制时,我无法将其粘贴到此处)

注意:

  • 我知道我的编程看起来很好但是我还是新手。
  • 我决定附上全班,因为问题可能在另一个地方。

编辑:

我的代码太大而无法调查,所以这里是相关部分:

private void startAlgo()
{

                  checkAvailTrailers();
                        if (forloopBreak == false)
                        {
                            setErrorMessage("Avail Trailers not enough!");
                        }
}

private void checkAvailTrailers()
    {

    string trailerRouteAllocation = null;
    string trailerVragAllocation = null;
    string tempHolderTrailer = null;
    string myNewTempT = null;
    string trailermyTemp = null;

    int trailerCount = 0;
    int tempTra = -1;

    //Gets trailer route classification
    using (SqlCommand selectTrailer = new SqlCommand("SELECT [TR_Routes] FROM dbo.TrailerDetail  WHERE [TR_Allocation] = " + 0, con))
    {
        using (SqlDataReader reader = selectTrailer.ExecuteReader())
        {
            while (reader.Read())
            {
                trailerRouteAllocation = reader.GetString(0);
                tempHolderTrailer = trailerRouteAllocation;
                myNewTempT = trailerRouteAllocation;

                for (int l = 0; l < tempHolderTrailer.Length; l++)
                {
                    tempTra = myNewTempT.IndexOf(",");
                    if (tempTra >= 0)
                    {
                        trailermyTemp = myNewTempT.Substring(0,tempTra);
                    }
                    else
                    {
                        trailermyTemp = myNewTempT;
                        if (trailermyTemp == myCurrentBookingRoute.ToString())
                        {
                            mycurrentTrailerAvailableRoute[trailerCount] = tempHolderTrailer;
                        }
                        break;

                    }
                    myNewTempT = myNewTempT.Substring(tempTra + 1);
                    if (trailermyTemp == myCurrentBookingRoute.ToString())
                    {
                        mycurrentTrailerAvailableRoute[trailerCount] = trailerRouteAllocation;
                    }
                }
                trailerCount++;
            }
            reader.Close();
        }
    }
    //gets trailer vrag classification.
    int countTrailerVrag = 0;
    for (int l = 0; l < mycurrentTrailerAvailableRoute.Length; l++)
    {
        if (mycurrentTrailerAvailableRoute[l] != null)
        {
            using (SqlCommand select = new SqlCommand("Select [TR_Classification] FROM dbo.TrailerDetail WHERE [TR_Routes] = '" + mycurrentTrailerAvailableRoute[l] + "' AND [TR_Allocation] = " + 0, con))
            {
                using (SqlDataReader readerS = select.ExecuteReader())
                {
                        while (readerS.Read())
                        {
                            trailerVragAllocation = readerS.GetString(0);
                            myAvailableTrailerVragClassification[countTrailerVrag] = trailerVragAllocation;
                            countTrailerVrag++;
                        }
                        readerS.Close();
                }
            }
        }
    }

    int countTrailers = 0;
    string trailerRegNum = null;
    for (int l = 0; l < mycurrentTrailerAvailableRoute.Length; l++)
    {
        if (mycurrentTrailerAvailableRoute[l] != null)
        {
            using (SqlCommand selectT = new SqlCommand("Select [TR_RegNumber] FROM dbo.TrailerDetail WHERE [TR_Routes] = '" + mycurrentTrailerAvailableRoute[l] + "'" + " AND [TR_Classification] = '" + myAvailableTrailerVragClassification[l] + "' AND [TR_Allocation] = " + 0, con))
            {
                 SqlDataReader readerT = selectT.ExecuteReader();
                    if (readerT.HasRows)
                    {
                        while (readerT.Read())
                        {
                            trailerRegNum = readerT.GetString(0);
                            myAvailableCurrentTraillerRegNumber[countTrailers] = trailerRegNum;
                            countTrailers++;
                        } 
                    }
                    else
                    {
                        forloopBreak = false;
                    }
                readerT.Close();
            }
        }
    }//END OF TRAILER CHECKING

    //gets trailer's max tonnage
    int myTrailerTonMax = 0;
    int myTrailerTon = 0;

    for (int l = 0; l < mycurrentTrailerAvailableRoute.Length; l++)
    {
        if (mycurrentTrailerAvailableRoute[l] != null)
        {
            using (SqlCommand selectT = new SqlCommand("Select [TR_MaxTonnage] FROM dbo.TrailerDetail WHERE [TR_Routes] = '" + mycurrentTrailerAvailableRoute[l] + "'" + " AND [TR_Classification] = '" + myAvailableTrailerVragClassification[l] + "'", con))
            {
                using (SqlDataReader readerS = selectT.ExecuteReader())
                {
                    while (readerS.Read())
                    {
                        myTrailerTon = readerS.GetInt32(0);
                        myTrailerAvailableTonMax[myTrailerTonMax] = myTrailerTon;
                        myTrailerTonMax++;
                    }
                    readerS.Close();
                }
            }
        }
    }
}

额外: - 我的数据库中的数据符合条件,while循环甚至执行,但最后我的布尔值返回false。

1 个答案:

答案 0 :(得分:0)

如果读者最初有任何数据,readerT.HasRows子句将始终为true。以下是MSDN page

中属性的说明
  

获取一个值,该值指示SqlDataReader是否包含一行或多行。

forloopBreak = false;循环结束时,您想要做的事情(我假设)设置为while。所以在使用块中你放了这个:

SqlDataReader readerT = selectT.ExecuteReader();
while (readerT.Read())
{
     trailerRegNum = readerT.GetString(0);
     myAvailableCurrentTraillerRegNumber[countTrailers] = trailerRegNum;
     countTrailers++;
} 
forloopBreak = false;
readerT.Close();

让我知道这是否有效!