2

私は C# で "while" ループを実行しています。これは、DB からいくつかのレコードを取得しています。ループの最後のレコードを検出/検索する最良の方法は何ですか? これは可能ですか?

これが私のコードです:

    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 から引き出される最後のレコードのコンマを削除する必要があります。

ありがとうございました

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

さらに別のアプローチ

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