我在C#中执行“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中提取的最后一条记录上的逗号。
谢谢
答案 0 :(得分:3)
反过来说:
bool is_first = true;
while (sdr.Read()) {
if (is_first) {
is_first = false;
} else {
writer.Write(",");
}
// Do your other writes here
}
答案 1 :(得分: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();
答案 2 :(得分: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);
答案 3 :(得分:0)
又一种方法
if(sdr.Read()) {
while (true) {
...
writer.WriteLine("[");
...
if (!sdr.Read()) {
writer.WriteLine("]");
break;
}
writer.WriteLine("],");
}
}
答案 4 :(得分:-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
动态剪裁长度。