2014-09-19 101 views
1

我正在使用以下來遍歷日期範圍並從文本文件導入到數據庫中。我想知道如何告訴代碼如果文件不會退出,則繼續下一個文件?如果循環失敗則繼續

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

umm ... if(File.Exists(...))'循環中的代碼?你有什麼嘗試? – BradleyDotNET 2014-09-19 21:30:38

回答

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

string file_name =行之後添加此行。放置在這裏可以防止對您的MySqlBulkLoader進行不必要的初始化。

continue將跳過其餘的代碼,並擊中循環中的下一個項目。一個例子見c# reference