C# 'Continue' 未按预期运行
C# 'Continue' not functioning as expected
我有以下代码
private void bgwSendMail_DoWork(object sender, DoWorkEventArgs e)
{
DataSet ds = getMailToSend();
DataTable table = ds.Tables[0];
{
foreach (DataRow row in table.Rows)
{
{
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
string attachment2 = ds.Tables[0].Rows[0]["Attachment2"].ToString();
string attachment3 = ds.Tables[0].Rows[0]["Attachment3"].ToString();
string attachment4 = ds.Tables[0].Rows[0]["Attachment4"].ToString();
string mailTo = ds.Tables[0].Rows[0]["EmailTo"].ToString();
string mailSubject = ds.Tables[0].Rows[0]["EmailSubject"].ToString();
string mailBody= ds.Tables[0].Rows[0]["EmailBody"].ToString();
string uid = ds.Tables[0].Rows[0]["uid"].ToString();
if (String.IsNullOrEmpty(attachment1))
{
//TODO Send Email Straight away ignore rest
}
else
{
if (!String.IsNullOrEmpty(attachment1))
{
bool attachment1Exists = checkFileExists(attachment1);
if (attachment1Exists == false)
{
continue;
}
}
现在我希望,当我们在底部点击继续(它确实被点击)时,我们应该退出回到 foreach,如下所示,并继续到数据集中的下一条记录
这不会发生,它会遍历 continue 的同一条记录,这正常吗?
如果正常,让 foreach
在数据表中退出一次后忽略该行的最佳方法是什么?
continue
正在按预期工作。
您正在枚举 table 中的所有行,但您没有使用它。相反,您总是访问 table:
中的第一行
DataTable table = ds.Tables[0];
foreach(DataRow row in table.Rows)
{
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
// ...
}
您一直在访问 ds.Tables[0].Rows[0]
。
您应该使用此代码:
foreach(DataRow row in table.Rows)
{
string attachment1 = row["Attachment1"].ToString();
// ...
}
所以你实际上是在按预期枚举 table 中的所有行,这不是一个无限循环,但你并没有使用 table 中的每一行,而是只使用了第一行。
改变
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
至
string attachment1 = row["Attachment1"].ToString();
以及对 DataRow 的所有其他后续引用。
我有以下代码
private void bgwSendMail_DoWork(object sender, DoWorkEventArgs e)
{
DataSet ds = getMailToSend();
DataTable table = ds.Tables[0];
{
foreach (DataRow row in table.Rows)
{
{
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
string attachment2 = ds.Tables[0].Rows[0]["Attachment2"].ToString();
string attachment3 = ds.Tables[0].Rows[0]["Attachment3"].ToString();
string attachment4 = ds.Tables[0].Rows[0]["Attachment4"].ToString();
string mailTo = ds.Tables[0].Rows[0]["EmailTo"].ToString();
string mailSubject = ds.Tables[0].Rows[0]["EmailSubject"].ToString();
string mailBody= ds.Tables[0].Rows[0]["EmailBody"].ToString();
string uid = ds.Tables[0].Rows[0]["uid"].ToString();
if (String.IsNullOrEmpty(attachment1))
{
//TODO Send Email Straight away ignore rest
}
else
{
if (!String.IsNullOrEmpty(attachment1))
{
bool attachment1Exists = checkFileExists(attachment1);
if (attachment1Exists == false)
{
continue;
}
}
现在我希望,当我们在底部点击继续(它确实被点击)时,我们应该退出回到 foreach,如下所示,并继续到数据集中的下一条记录
这不会发生,它会遍历 continue 的同一条记录,这正常吗?
如果正常,让 foreach
在数据表中退出一次后忽略该行的最佳方法是什么?
continue
正在按预期工作。
您正在枚举 table 中的所有行,但您没有使用它。相反,您总是访问 table:
中的第一行DataTable table = ds.Tables[0];
foreach(DataRow row in table.Rows)
{
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
// ...
}
您一直在访问 ds.Tables[0].Rows[0]
。
您应该使用此代码:
foreach(DataRow row in table.Rows)
{
string attachment1 = row["Attachment1"].ToString();
// ...
}
所以你实际上是在按预期枚举 table 中的所有行,这不是一个无限循环,但你并没有使用 table 中的每一行,而是只使用了第一行。
改变
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
至
string attachment1 = row["Attachment1"].ToString();
以及对 DataRow 的所有其他后续引用。