5

Is there any form of statement which converts a datarow to a CSV line so it could be written to a .csv file? (with or without steps in between)

Dashzapp
  • 123
  • 4
  • 13
  • 1
    Have you looked at this question [Writing a CSV file in .net](http://stackoverflow.com/questions/1684667/writing-a-csv-file-in-net)(specifically [this answer](http://stackoverflow.com/a/2315185/164966)), between that and [these examples](http://msdn.microsoft.com/en-us/library/bb386916.aspx) of Linq to DataSets, you could probably write the export in under 10 lines of code. – Roman Mar 08 '12 at 07:08

4 Answers4

5

Try with following code:

StringBuilder sb = new StringBuilder(); 

var columnNames = dt.Columns.Cast<DataColumn>().Select(column =>     column.ColumnName).ToArray();
sb.AppendLine(string.Join(",", columnNames));

foreach (DataRow row in dt.Rows)
{
  var fields = row.ItemArray.Select(field => field.ToString()).ToArray();
  sb.AppendLine(string.Join(",", fields));
}

File.WriteAllText("test.csv", sb.ToString());
  • where do u get the problem????Could u please elaborate me where are u getting problem??? –  Mar 08 '12 at 09:27
  • I'm still testing, no need of them infinite question marks! I get errors at var columnnames = dt.columns <- and dt.Rows <- – Dashzapp Mar 08 '12 at 09:35
  • Did you import something from the System class? – Dashzapp Mar 08 '12 at 09:58
  • Are u talking about namespaces? –  Mar 08 '12 at 10:02
  • Yes exactly, let me post the error:Error 2 'System.Data.DataSet' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?) The other one is exactly the same, just using columns instead of rows – Dashzapp Mar 08 '12 at 10:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8663/discussion-between-akash88-and-dashzapp) –  Mar 08 '12 at 10:08
1

The below works for me.

foreach (DataRow row in rows)
{
    string line = "";
    for (int i = 0; i < row.ItemArray.Length; i++)
    {
         line += row[i] + ",";  
    }
    line = line.TrimEnd(',');

}
Christopher Smit
  • 953
  • 11
  • 27
0

A quick Google search turned this up: http://www.rcs-solutions.com/blog/2009/01/15/ConvertDataTableToCSVViaExtensionMethod.aspx

It's a bit dated. There is no native way to do it, so you'll need to either iterate over the datatable using looping constructs, but I'd recommend using LINQ methods as it will be much cleaner.

ctorx
  • 6,841
  • 8
  • 39
  • 53
0

I was looking at this the other day:

According to the quickstart you can write code like so:

[DelimitedRecord(",")]
public class Customer
{
  public int CustId;    
  public string Name;
  public decimal Balance;
}
FileHelperEngine engine = new FileHelperEngine(typeof(Customer));
Customer[] res = engine.ReadFile("FileIn.txt") as Customer[];
engine.WriteFile("FileOut.txt", res);
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121