C#에서 "while"루프를 수행 중이며 일부 레코드가 DB에서 가져 오는 중입니다. 루프에서 마지막 레코드를 검색/찾는 가장 좋은 방법은 무엇입니까? 이것이 가능한가? "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 ("]);": 여기
내 코드입니다 DB에서 가져온 마지막 레코드에서 해당 쉼표를 제거해야합니다. 이bool is_first = true;
while (sdr.Read()) {
if (is_first) {
is_first = false;
} else {
writer.Write(",");
}
// Do your other writes here
}
은'sdr'은 무엇을? –
@EvanMulawski - 아마도'SqlDataReader'입니다. – Oded
팁, 축약 된 문자열을 사용하여 모든 백 슬래시를 저장하십시오.'@ "\ foo ..." – gdoron