I am able to escape comma between strings with using double quotes such as strings are:"Laxchaman","Bharath","satrudhan" was brother of Ram.It is working fine in csv file But when i am copy the string from the csv cell to notepad it is showing """Laxchaman"",""Bharath"",""satrudhan"" was brother of Ram". That's why the csv file is not being upload in SQL. I want String must be write in csv file with comma as same as sentences in real form.
- using (DataTable dt = new DataTable())
- {
- dt.Load(reader);
- Console.WriteLine(dt.Rows.Count);
- StringBuilder sb = new StringBuilder();
- string sbb = "";
- sb.AppendLine(string.Join(",", dt.Columns.Cast<datacolumn>().Select(c => c.ColumnName)));
- sb.Remove(sb.Length - 1, 1);
- foreach (DataRow row in dt.Rows)
- {
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- if (row[i].ToString().Contains(","))
- {
- sb.Append(String.Format("\"{0}\",", row[i]));
- }
- else
- {
- sb.Append(row[i].ToString() + ",");
- }
- }
- sb.AppendLine();
- }
- ArrayList ar = new ArrayList();
- File.WriteAllText(@"D:\ISR\1" + ".csv", sb.ToString());
- }