关于我的计划:
我的算法(此类)用于检查交付是否已完成,之后使卡车/拖车/驱动程序可用于另一次交付,同时此算法在交付时发送另一辆卡车/拖车/驱动程序。总结一下,这个课程做了以下几点:
我的问题:
我不知道我的班级有什么问题,我已经花了好几个小时,似乎无法找出导致我的布尔变量(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。
答案 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();
让我知道这是否有效!