I have the below code that exports to csv fine, but just realised it doesn't handle commas or line breaks in the 'comments' field textbox. Is there a way to ignore commas and line breaks in a particular field or in all of them?
private void saveFileDialogTrack_FileOk(object sender, CancelEventArgs e)
{
StringBuilder str = new StringBuilder();
foreach (DataRow dr in this.trackDataSet.Track)
{
foreach (object field in dr.ItemArray)
{
str.Append(field.ToString() + ",");
}
str.Replace(",", Environment.NewLine, str.Length - 1, 1);
}
string name = saveFileDialogTrack.FileName;
try
{
System.IO.File.WriteAllText(name, str.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
str = null;
}
Sample csv (first few fields) Barbagallo,31,39,51.01,
private void openFileDialogTrack_FileOk(object sender, CancelEventArgs e)
{
try
{
using (StreamReader SR = new StreamReader(openFileDialogTrack.FileName))
{
while (!SR.EndOfStream)
{
var CSValues = SR.ReadLine().Split(',');
SqlCeConnection myConnection = new SqlCeConnection("Data Source = Track.sdf");
myConnection.Open();
String str = (@"INSERT INTO Track (Track, StartFinishLineNSDegrees, StartFinishLineNSMinutes,
......................
VALUES (@track, @SFNSD, @SFNSM,
......................
SqlCeCommand cmd = new SqlCeCommand(str, myConnection);
cmd.Parameters.AddWithValue("@track", CSValues[0] == string.Empty ? (object)DBNull.Value : CSValues[0]);
cmd.Parameters.AddWithValue("@SFNSD", CSValues[1] == string.Empty ? (object)DBNull.Value : CSValues[1]);
cmd.Parameters.AddWithValue("@SFNSM", CSValues[2] == string.Empty ? (object)DBNull.Value : CSValues[2]);