title: Format table to text output in C#
body: <p>I have a query versus an SQL database in C#. From this query I get a List like:</p>
  
  <pre><code>List&lt;List&lt;string&gt;&gt;()
  </code></pre>
  
  <p>this list can be changed to any other table type if required..</p>
  
  <p>In Microsoft Server Management Studio it looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/KkAXP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KkAXP.png" alt="enter image description here"></a></p>
  
  <p>In Microsoft SQL Server Management Studio you can simply output the result as text, which looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/snBOG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/snBOG.png" alt="enter image description here"></a></p>
  
  <p>Now as I am automating some tasks I will use C# rather than manually using MSSQL. Due to a GXP environment it is important to have every datachange in the Database Documented separately with a text report attached to a "ticket".</p>
  
  <p>For now my query with Table Text generation looks like this:</p>
  
  <pre><code>SqlCommand command = new SqlCommand("SELECT '" + description + "' AS '" + description + "', CPPS_Site,Study_Id,CustNo,CPPS_Job FROM CDS.dbo.Studies " +
                                              "WHERE Study_Id = " + studyID, con);
  List&lt;string&gt; overview = new List&lt;string&gt;();
  
  overview.Add("|=========================================================|");
  overview.Add("|"+description + "\t|CPPS_Site\t|Study_ID\t|CustNo\t|CPPS_Job|");
  overview.Add("|---------------------------------------------------------|");
  using (SqlDataReader resultQuery = command.ExecuteReader())
  {
      var schemaTable = resultQuery.GetSchemaTable();
      while (resultQuery.Read())
      {
          string build = "|";
          build +=  resultQuery.GetSqlValue(0) + "\t|"
                + resultQuery.GetSqlValue(1) + "\t\t|"
                + resultQuery.GetSqlValue(2) + "\t\t|"
                + resultQuery.GetSqlValue(3) + "\t|"
                + resultQuery.GetSqlValue(4);
          build += "|";
          overview.Add(build);
       }
  overview.Add("|=========================================================|");
       return overview;
   }
  </code></pre>
  
  <p>Result:</p>
  
  <p><a href="https://i.stack.imgur.com/yuniM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yuniM.png" alt="enter image description here"></a></p>
  
  <p>while this is technically working and would also be accepted, I am personally very unpleased with this ugly solution. For instance with strings of different length, the format will not fit anymore. Do you know a more clean solution?</p>