Apologies if this question is a duplicate.
I'm trying to create a CSV file with, as an example, a Title, Name and Comments column. Each row of the CSV has it's values for each column being read from the Database into the relevant variable, and these variables, split by commas, are then added to a variable 'newline' which was then appended to the csv as follows:
string title = reader[0]
string name = reader[1]
string comments = reader[2]
var csv = new StringBuilder();
var headerLine = "Title,Name,Comments"
csv.AppendLine(headerLine);
var newline = title + "," + name + "," + comments;
csv.AppendLine(newline);
Originally I took the above approach, but quickly ran into the problem where, if any of the fields read in from the database had commas in them, that field would be split in the middle, e.g. if comments = "Comment, goes, here" would be spread across 3 columns in the out CSV instead of 1.
To try and overcome this problem, I changed my code so that all fields would be wrapped in double quotes, meaning commas within a field would no longer split the CSV, as follows:
var newline = "\"" + title + "\",\"" + name + "\",\"" + comments + "\""
Now, with this in place, I am getting a problem where if any of the values read in from the Database contain a double quote, this messes up everything, since a double quote from the database is displayed as \" when the field is converted into a string, which is what I'm already using to wrap the start and end of each field in double quotes to prevent commas splitting fields containing commas.