title: Get data type of a SQL Server column using C#
body: <p>I'm not sure what exactly I'm doing wrong, can someone correct it please? I need to determine the type of a retrieved column from a SQL Server database using C#. </p>
  
  <p>Say, I have this:</p>
  
  <pre><code>SqlConnection cn = new SqlConnection("Sql Connection String");
  SqlCommand cmd = new SqlCommand("SELECT * FROM [TableName]", cn);
  SqlDataReader rdr = cmd.ExecuteReader();
  
  while (rdr.Read())
  {
      for (int c = 0; c &lt; rdr.VisibleFieldCount; c++)
      {
          System.Type type = rdr.GetFieldType(c);
  
          //So can I do this? (Pseudo-code)
          //switch(type)
          //{
          //case string:
          //case int:
          //case DateTime:
          //etc.
          //}
      }
  }
  </code></pre>