如果失败则继续循环

时间:2014-09-19 21:29:05

标签: c# .net loops for-loop

我使用以下内容遍历日期范围并从文本文件导入数据库。我想知道我怎么能告诉代码如果文件没有超出继续下一个文件?

for (DateTime date = last_date; date.Date <= DateTime.Today; date = date.AddDays(1))
{
  string file_name = "H" + date.ToString("yyyy-MM-dd") + ".TXT";

  MySqlBulkLoader bl = new MySqlBulkLoader(sql_connection);
  bl.TableName = "processing_table";
  bl.FieldTerminator = ",";
  bl.LineTerminator = "\n";
  bl.FileName = @"C:\Temp\Attendence\" + file_name;
  bl.NumberOfLinesToSkip = 0;

  int count = bl.Load();

  string sql = "SELECT source, created_date, created_time, company, action, employee_number, employee_name, outlet, field_1, field_2, field_3, field_4 FROM processing_table";
  MySqlCommand cmd = new MySqlCommand(sql, sql_connection);
  MySqlDataReader rdr = cmd.ExecuteReader();

  while (rdr.Read())
  {
      SplashScreenManager.Default.SetWaitFormDescription(rdr[1] + " - " + rdr[2]);
  }

  rdr.Close();
}

1 个答案:

答案 0 :(得分:0)

if (!File.Exists(@"C:\Temp\Attendence\" + file_name)) { continue; }

string file_name =行之后添加此行。将其置于此处可防止对MySqlBulkLoader进行不必要的初始化。

continue将跳过剩下的代码并点击循环中的下一个项目。有关示例,请参阅c# reference