title: Determine first sheet name of an Excel file when reading xls as a database
body: <p>I have a need to read in some client data from an Excel sheet. I found it very quick and easy to read the <code>*.xls</code> as data, this way:</p>
  
  <pre><code>string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyExcelFile.xls;Extended Properties=\"Excel 8.0;HDR=YES\"";
  using (var conn = new System.Data.OleDb.OleDbConnection(connString)) {
      conn.Open();
      System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select * From [SheetName$]", conn);
      OleDbDataReader reader = cmd.ExecuteReader();
      int firstNameOrdinal = reader.GetOrdinal("First Name");
      int lastNameOrdinal = reader.GetOrdinal("Last Name");
      while (reader.Read()) {
          Console.WriteLine("First Name: {0}, Last Name: {1}", 
              reader.GetString(firstNameOrdinal), 
              reader.GetString(lastNameOrdinal));
      }
  }
  </code></pre>
  
  <p>The problem comes in at the 4th line: <code>SELECT * FROM [SheetName$]</code>. In this example, you need to replace <code>"SheetName"</code> with the name of the worksheet you are trying to read. Most of the time, the client names this the same, and all is fine. </p>
  
  <p>But sometimes they don't, and that means some manual work on our part to fix it. There is always just one sheet (in this case anyway) and so I'd rather just tell the code to "use the first sheet, whatever it is named". </p>
  
  <p>I haven't been able to find a way to accomplish this using this technique, so I'm reaching out to the community. Thanks in advance for any advice.</p>
  
  <p><strong>PS</strong> - We have to go through a length approval process for external files (e.g. FileHelpers) and I prefer using this process because it does not require a third-party library. I'd prefer to limit answers to this method if possible. </p>
  
  <p>Thanks.</p>