andrew4582

TableToCSV

Jul 2nd, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1.         void TableToCSV(TextWriter output,DataTable table) {
  2.             string FLD_FMT = "{0},";
  3.             foreach(DataColumn column in table.Columns) {
  4.                 output.Write(string.Format(FLD_FMT,column.ColumnName));
  5.             }
  6.             output.WriteLine();
  7.  
  8.             foreach(DataRow row in table.Rows) {
  9.  
  10.                 foreach(DataColumn column in table.Columns) {
  11.                     object value = null;
  12.                     if(row.IsNull(column))
  13.                         value = "NULL";
  14.                     else
  15.                         value = row[column];
  16.                     output.Write(string.Format(FLD_FMT,value));
  17.                 }
  18.                 output.WriteLine();
  19.             }
  20.         }
  21.  
  22.         void TableToCSV(StringBuilder output,DataTable table) {
  23.             string FLD_FMT = "{0},";
  24.             foreach(DataColumn column in table.Columns) {
  25.                 output.AppendFormat(FLD_FMT,column.ColumnName);
  26.             }
  27.  
  28.             output.AppendLine();
  29.             foreach(DataRow row in table.Rows) {
  30.  
  31.                 foreach(DataColumn column in table.Columns) {
  32.                     object value = null;
  33.                     if(row.IsNull(column))
  34.                         value = "NULL";
  35.                     else
  36.                         value = row[column];
  37.                     output.AppendFormat(FLD_FMT,value);
  38.                 }
  39.                 output.AppendLine();
  40.             }
  41.         }
Advertisement
Add Comment
Please, Sign In to add comment