英文:
Move On To Next Record If Condition Not Met
问题
如果列x的值为null,你可以在代码中使用continue
语句来跳过当前记录,然后继续处理下一条记录。以下是相应的代码部分:
using (OleDbDataReader dr = cmd.ExecuteReader())
{
try
{
while (dr.Read())
{
if (!dr.IsDBNull(0))
{
ID = dr.GetString(0).ToString();
}
if (!dr.IsDBNull(1))
{
REFERENCE = dr.GetString(1);
}
else
{
// 如果列1的值为null,跳过当前记录,继续下一条记录
continue;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
dr.Close();
dr.Dispose();
con.Close();
}
}
这样,当列x的值为null时,会跳过当前记录并继续处理下一条记录。
英文:
I am trying to move on to the next record in the DataReader if a condition is not met. I am aware that DataReaders are foward-only, so what I am trying to achieve is; while data read, move on to the next record if column x has a null value;
using (OleDbDataReader dr = cmd.ExecuteReader())
{
try
{
while (dr.Read())
{
if (!dr.IsDBNull(0))
{
ID = dr.GetString(0).ToString();
}
if (!dr.IsDBNull(1))
{
REFERENCE = dr.GetString(1);
}
else
{
//this is where I need it to 'abandon' the current record and move on to the next...
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
dr.Close();
dr.Dispose();
con.Close();
}
}
I have only put in the part of the code which is relevant so that it's short and straight to the point. Any guidance would be greatly appreciated.
答案1
得分: 2
要在不满足条件时跳过DataReader
中的当前记录并继续下一条记录,您可以使用continue
语句。continue
语句允许您跳过当前循环迭代中的其余代码并继续下一次迭代。以下是如何修改您的代码:
using (OleDbDataReader dr = cmd.ExecuteReader())
{
try
{
while (dr.Read())
{
if (!dr.IsDBNull(0))
{
ID = dr.GetString(0);
}
if (!dr.IsDBNull(1))
{
REFERENCE = dr.GetString(1);
}
else
{
// 跳过当前记录并继续下一个
continue;
}
// 处理当前记录的其余代码部分
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
dr.Close();
dr.Dispose();
con.Close();
}
}
通过使用continue
,当满足列1的IsDBNull
条件时,代码将跳过当前循环迭代中的其余代码并继续下一次迭代,有效地移动到DataReader
中的下一条记录。
英文:
To skip the current record in the DataReader
and move on to the next record if a condition is not met, you can use the continue
statement. The continue
statement allows you to skip the remaining code within the current iteration of the loop and move on to the next iteration. Here's how you can modify your code:
using (OleDbDataReader dr = cmd.ExecuteReader())
{
try
{
while (dr.Read())
{
if (!dr.IsDBNull(0))
{
ID = dr.GetString(0);
}
if (!dr.IsDBNull(1))
{
REFERENCE = dr.GetString(1);
}
else
{
// Skip the current record and move on to the next
continue;
}
// Rest of your code for processing the current record
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
dr.Close();
dr.Dispose();
con.Close();
}
}
By using continue
, when the IsDBNull
condition for column 1 is met, the code will skip the remaining code within the current iteration of the loop and move on to the next iteration, effectively moving to the next record in the DataReader
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论