none
StreamWriter RRS feed

  • Question

  • Dear All,

    i have the datatable returns from sql store procedure:
    sproResults = myUtils.ExecSproc(objSession, "spVoucherDetails_SelPending", printRunNumber);

    and i loop through this table and try to write each row into a .csv file and save into disk, i know my current implementation wasn't quite right in here. Can someone plz advise me?

    foreach
    (DataRow row in sproResults.Tables[0].Rows)
    {
        FileInfo fileName =
    new FileInfo("PendingVoucher.csv");
        StreamWriter stream = fileName.CreateText();
        stream.Write(row);
        stream.Close();
    }

    Thanks heaps.

    Wednesday, February 18, 2009 6:05 AM

Answers

  •  Your code will overwrite the file for each data row. Move the declaration of the StreamWriter outside of the loop. StreamWriter can also accept a path in its constructor so the FileInfo is not required.

    using (StreamWriter writer = new StreamWriter("PendingVoucher.csv"))
    {// using ensures the writer is disposed afterwards. The Close method at the end might not be required, I'm too tired to check:)
      foreach(DataRow row in sproResults.Tables[0].Rows)
      {
        writer.Write(row); // You may need to format this manually... I don't think it will come out comma-delimited automatically.
        //writer.Write(formatDataRow(row));
      }
      writer.Close();
    }

    If you need to format the row then I'd recommend creating a small function that takes the datarow as a parameter, constructs an array of column values and performs a string.Join() using a comma for a delimiter. This better reflects SRP (single responsibility principle) and makes it easy to modify if you want to do something later like exclude or format certain columns.
    It should be straight-forward enough to write a method formatDataRow() to do that.

    #2 happens.
    • Proposed as answer by Matthew Watson Wednesday, February 18, 2009 9:51 AM
    • Marked as answer by AndieDu Wednesday, February 18, 2009 10:40 PM
    Wednesday, February 18, 2009 8:17 AM

All replies

  •  Your code will overwrite the file for each data row. Move the declaration of the StreamWriter outside of the loop. StreamWriter can also accept a path in its constructor so the FileInfo is not required.

    using (StreamWriter writer = new StreamWriter("PendingVoucher.csv"))
    {// using ensures the writer is disposed afterwards. The Close method at the end might not be required, I'm too tired to check:)
      foreach(DataRow row in sproResults.Tables[0].Rows)
      {
        writer.Write(row); // You may need to format this manually... I don't think it will come out comma-delimited automatically.
        //writer.Write(formatDataRow(row));
      }
      writer.Close();
    }

    If you need to format the row then I'd recommend creating a small function that takes the datarow as a parameter, constructs an array of column values and performs a string.Join() using a comma for a delimiter. This better reflects SRP (single responsibility principle) and makes it easy to modify if you want to do something later like exclude or format certain columns.
    It should be straight-forward enough to write a method formatDataRow() to do that.

    #2 happens.
    • Proposed as answer by Matthew Watson Wednesday, February 18, 2009 9:51 AM
    • Marked as answer by AndieDu Wednesday, February 18, 2009 10:40 PM
    Wednesday, February 18, 2009 8:17 AM
  • thansk a lot dude....
    Wednesday, February 18, 2009 10:40 PM