继续下一个记录,如果条件不满足。

huangapple go评论70阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月10日 20:45:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定