Write Rows from DataTable to Text File

前端 未结 6 1579
面向向阳花
面向向阳花 2021-02-14 11:31
public void GenerateDetailFile()
{
  if (!Directory.Exists(AppVars.IntegrationFilesLocation))
  {
    Directory.CreateDirectory(AppVars.IntegrationFilesLocation);
  }

          


        
6条回答
  •  长情又很酷
    2021-02-14 12:07

    There is no "natural" string representation for a DataRow. You need to write it out in whatever format you desire, i.e., comma-separated list of values, etc. You can enumerate the columns and print their values, for instance:

    foreach (DataRow row in table.Rows)
    {
        bool firstCol = true;
        foreach (DataColumn col in table.Columns)
        {
            if (!firstCol) sw.Write(", ");
            sw.Write(row[col].ToString());
            firstCol = false;
        }
        sw.WriteLine();
    }
    

提交回复
热议问题