2

我在 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("],");" 我需要删除从数据库中提取的最后一条记录上的逗号。

谢谢你

4

5 回答 5

3

反过来做:

bool is_first = true;

while (sdr.Read()) {

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

    // Do your other writes here
}
于 2012-06-28T19:28:54.033 回答
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();
于 2012-06-28T19:35:14.560 回答
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);
于 2012-06-28T19:45:15.967 回答
0

另一种方法

if(sdr.Read()) {
    while (true) {
        ...
        writer.WriteLine("[");  
        ...
        if (!sdr.Read()) {
            writer.WriteLine("]"); 
            break;
        }
        writer.WriteLine("],");  
    }
}
于 2012-06-28T19:49:40.067 回答
-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

于 2012-06-28T19:32:29.793 回答