title: How to get table name of a column from SqlDataReader
body: <p>I have an SQL query I get from a configuration file, this query usually contains 3-6 joins.</p>
  
  <p>I need to find at run time, based on the result set represented by SqlDataReader, to find the name of the table for each column.</p>
  
  <p>Here are some thing that don't work:</p>
  
  <ul>
  <li>SqlDataReader.GetName returns the column name but not the table name.</li>
  <li>SqlDataReader.GetSchemaTable returns a data table with column information - but all the table names are null.</li>
  <li>Querying information_schema doesn't help because I need data on the results of the current query (and the column names are not unique - there are columns with the same name in different tables).</li>
  </ul>
  
  <p>I'm using .net 3.5SP1/ C#/ SQL Server 2008 in a console application.</p>
  
  <p><strong>EDIT:</strong> I know this is not possible for all cases since a "column" can be combined from multiple tables, a function or even a constant expression - I'm looking for something that works in the simple case.</p>
  
  <p><strong>EDIT 2:</strong> Found out why it didn't work - You can use SqlDataReader.GetSchemaTable to get table information but you have to set CommandBehavior to KeyInfo, you do that in the ExecuteReader call:</p>
  
  <pre><code>reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);
  </code></pre>