2012-06-28 20 views
2

我在C#中執行「while」循環,這是通過從數據庫中拉出一些記錄。檢測/查找循環中最後一條記錄的最佳方法是什麼?這可能嗎?如何檢測/查找while循環的結束?

這裏是我的代碼:

while (sdr.Read()) 
    { 
     //Pull each line from DB 
     the_title = sdr["the_title"].ToString(); 
     the_cats = sdr["the_category"].ToString(); 
     the_tags = sdr["the_tags"].ToString(); 
     the_date = sdr["the_date"].ToString(); 

     //Start file creation 
     writer.WriteLine("["); 
     writer.WriteLine("\"" + the_title + "\", "); 
     writer.WriteLine("\"" + the_cats + "\", "); 
     writer.WriteLine("\"" + the_tags + "\", "); 
     writer.WriteLine("\"" + the_date + "\", "); 
     writer.WriteLine("\"<a href=\\\"#\\\" class=\\\"sepV_a\\\" title=\\\"Edit\\\"><i class=\\\"icon-pencil\\\"></i></a>\""); 

     writer.WriteLine("],"); 

    } 
    writer.WriteLine("]"); 
    writer.WriteLine("}"); 
    writer.Close(); 

我遇到的問題是與代碼的最後一行的「writer.WriteLine(」]「);」我需要刪除從數據庫中拉出的最後一個記錄上的逗號。

謝謝

+0

什麼是'sdr'? –

+0

@EvanMulawski - 可能是'SqlDataReader'。 – Oded

+0

提示:使用逐字字符串來保存你所有的反斜槓:'@「\ foo ...」' – gdoron

回答

-1

你不會知道最後的(你已經達到了,直到/過去了吧),但你可以知道第一。您可以通過修改代碼:

bool isFirst = true; 
while (sdr.Read()) 
{ 
    if (isFirst) isFirst = false; 
    else writer.WriteLine(","); 

    //Pull each line from DB 
    the_title = sdr["the_title"].ToString(); 
    the_cats = sdr["the_category"].ToString(); 
    the_tags = sdr["the_tags"].ToString(); 
    the_date = sdr["the_date"].ToString(); 

    //Start file creation 
    writer.WriteLine("["); 
    writer.WriteLine("\"" + the_title + "\", "); 
    writer.WriteLine("\"" + the_cats + "\", "); 
    writer.WriteLine("\"" + the_tags + "\", "); 
    writer.WriteLine("\"" + the_date + "\", "); 
    writer.WriteLine("\"<a href=\\\"#\\\" class=\\\"sepV_a\\\" title=\\\"Edit\\\"><i class=\\\"icon-pencil\\\"></i></a>\""); 

    writer.Write("]"); 
} 
writer.WriteLine(); 

否則,以避免在循環的每個實例的檢查,你可以使用:

var sb = new StringBuilder(); 
while (sdr.Read()) 
{ 
    //Pull each line from DB 
    the_title = sdr["the_title"].ToString(); 
    the_cats = sdr["the_category"].ToString(); 
    the_tags = sdr["the_tags"].ToString(); 
    the_date = sdr["the_date"].ToString(); 

    //Start file creation 
    sb.AppendLine("["); 
    sb.AppendLine("\"" + the_title + "\", "); 
    sb.AppendLine("\"" + the_cats + "\", "); 
    sb.AppendLine("\"" + the_tags + "\", "); 
    sb.AppendLine("\"" + the_date + "\", "); 
    sb.AppendLine("\"<a href=\\\"#\\\" class=\\\"sepV_a\\\" title=\\\"Edit\\\"><i class=\\\"icon-pencil\\\"></i></a>\""); 

    sb.AppendLine("],"); 
} 
if (sb.Length > 0) 
{ 
    // Write result, sans characters for last newline (Environment.NewLine) and comma. 
    writer.WriteLine(sb.ToString(0, sb.Length - (Environment.NewLine.Length + 1)); 
} 

編輯:製造裁剪長度動態使用Environment.NewLine.Length

+0

我需要在每一行上保留逗號,除了最後一行。這會幫我嗎? – jorame

+0

@jorame對不起,我忘了逗號。修正了,現在。我需要仔細檢查'Length - 2'是否是正確的裁剪數量,但是否則會執行此操作。 –

3

做它周圍的其他方法:

bool is_first = true; 

while (sdr.Read()) { 

    if (is_first) { 
     is_first = false; 
    } else { 
     writer.Write(","); 
    } 

    // Do your other writes here 
} 
+2

這是非常不高效的代碼。考慮將字符串分配給變量(最好是StringBuilder),並刪除最後一個逗號。 – Nas

+0

它和'writer.WriteLine(「[」);'是一樣高效。我不會浪費時間運行基準測試,但我相當有信心,這種差異可以忽略不計。 –

+0

這是關於處理器級別的分支預測。這可能會幫助你理解:http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array – Nas

2

我建議你只刪除最後一個字符。這是循環內最有效的解決方案。

StringBuilder sb = new StringBuilder(); 

    while (sdr.Read()) 
    { 
     sb.Append("Value"); 
     .... 
    } 

if(sb.Length > 0) 
{ 
sb.Remove(sb.Length - 1, 1) 
} 
var result = sb.ToString(); 
0

另一種方法,應該工作:

List<String> bufferList = new List<String>(); 
while (sdr.Read()) 
{ 
    //Pull each line from DB 
    the_title = sdr["the_title"].ToString(); 
    the_cats = sdr["the_category"].ToString(); 
    the_tags = sdr["the_tags"].ToString(); 
    the_date = sdr["the_date"].ToString(); 

    StringBuilder tempSb = new StringBuilder(); 
    tempSb.AppendLine(); 

    //Start file creation 
    tempSb.AppendLine("["); 
    tempSb.AppendLine("\"" + the_title + "\", "); 
    tempSb.AppendLine("\"" + the_cats + "\", "); 
    tempSb.AppendLine("\"" + the_tags + "\", "); 
    tempSb.AppendLine("\"" + the_date + "\", "); 
    tempSb.AppendLine("\"<a href=\\\"#\\\" class=\\\"sepV_a\\\" title=\\\"Edit\\\"><i class=\\\"icon-pencil\\\"></i></a>\""); 
    tempSb.AppendLine(("]"); 

    bufferList.Add(tempSb.ToString()); 

} 

String.Join(",", bufferList); 
0

另一種方法

if(sdr.Read()) { 
    while (true) { 
     ... 
     writer.WriteLine("["); 
     ... 
     if (!sdr.Read()) { 
      writer.WriteLine("]"); 
      break; 
     } 
     writer.WriteLine("],"); 
    } 
}