0

First time trying to build a csv file in c#, here is the code:

string FilePath = @"C:\Users\me\Downloads\csvfiles\test-" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".csv";
string delimit = ",";
List<string> cells = new List<string>();

Console.WriteLine("Gathering the data...");

SqlConnection con = new SqlConnection(Properties.Settings.Default.ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT id, name FROM contacts";

SqlDataReader reader = cmd.ExecuteReader();

Console.WriteLine("Building the csv file");

while (reader.Read())
{
    AddCell((IDataRecord)reader, cells);
}

StringBuilder sb = new StringBuilder();

for (int i = 0; i < cells.Count; i++)
{
    sb.AppendLine(string.Join(delimit, cells[i]));
}

File.WriteAllText(FilePath, sb.ToString());

/*Method to add each record to the list*/
public static void AddCell(IDataRecord record, List<string> cells)
{
    cells.Add(record[0].ToString());
    cells.Add(record[1].ToString());
}

Trouble is, it appends everything to a new row, it doesn't move record[1] to the next column and start a new row...

3
  • Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. Commented Mar 6, 2014 at 12:26
  • I am joining by the delimit variable, which is why it is moving to the next row... Commented Mar 6, 2014 at 12:33
  • If you discovered a solution to your problem, post it as an answer, please. Or delete your question completely. Commented Mar 6, 2014 at 12:45

2 Answers 2

1

Your For loop is incorrect for what you are trying to do.

Replace :

    for (int i = 0; i < cells.Count; i++)
    {
         sb.AppendLine(string.Join(delimit, cells[i]));
    }

With

    int i = 0;
    while (i < cells.Count)
    {
       sb.AppendLine(string.Join(delimit, cells[i],cells[i+1]));
       i = i + 2;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

@user2405469 : If this solved your problem, please mark it as answer.
0
string FilePath = @"C:\Users\me\Downloads\csvfiles\test-" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".csv";
string delimit = ",";
List<string> cells = new List<string>();

Console.WriteLine("Gathering the data...");

SqlConnection con = new SqlConnection(Properties.Settings.Default.ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT id, name FROM contacts";

SqlDataReader reader = cmd.ExecuteReader();

Console.WriteLine("Building the csv file");

while (reader.Read())
{
    AddCell((IDataRecord)reader, cells);
}

StringBuilder sb = new StringBuilder();

for (int i = 0; i < cells.Count; i++)
{
    if (cells[i].Equals("\n"))
    {
        sb.Append(Environment.NewLine);
    }
    else
    {
        sb.Append(cells[i]);
    }
}

File.WriteAllText(FilePath, sb.ToString());

/*Method to add each record to the list*/
public static void AddCell(IDataRecord record, List<string> cells)
{
    cells.Add(record[0].ToString());
    cells.Add(",");
    cells.Add(record[1].ToString());
    cells.Add("\n");
}

not as elegant as yours @cvraman but it worked too :) I will be using your solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.