0
               _conn = new OleDbConnection(_connectionStrting);
                _conn.Open();
                DataTable dt = _conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                String[] sheetNames = new String[dt.Rows.Count];
                int i = 0;

                foreach (DataRow row in dt.Rows)
                {
                    sheetNames[i] = row["TABLE_NAME"].ToString();
                    comboBox2.Items.Add(sheetNames[i]);
                    i++;
                }

                _conn.Close();

this code works for me but I want to know if have a solution with OleDbDataAdapter

soroosh elyasi
  • 116
  • 1
  • 6
  • Possible duplicate of [Fill DataSet from OleDbDataAdapter](https://stackoverflow.com/questions/15735184/fill-dataset-from-oledbdataadapter) – Markus Jul 24 '17 at 14:26
  • Welcome to SO! See here on how to post a good question: https://stackoverflow.com/help/how-to-ask – Robert Moskal Jul 24 '17 at 14:34

1 Answers1

2

OleDbDataAdaptor has nothing to do with Sheet names; Sheet names belong to Excel.Workbook, and Excel Workbook belongs to Excel.Application. You would need to iterate through workbook sheet names:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xl = new Excel.Application();;
Excel.Workbook wb = xl.Workbooks.Open("WorkBookfullPath", 0, true);

foreach (Excel.Worksheet ws in wb.Worksheets) {
        {
             string wsName = ws.Name;  
        }

You don't really need here OleDbDataAdapter, in this case (in case you need to read data from worksheet) you can just read from Excel into 2 dimentional array (1st dimension is rows and second dimension is columns):

object[,] data = ws.UsedRange.Value2; // change UsedRange range to your table range, and  you can also use ws.UsedRange.FormulaR1C1
Richard Mneyan
  • 656
  • 1
  • 13
  • 20
  • You are welcome. Also remember, dealing with Microsoft.Office.Interop.Excel could be tricky. Read here how to properly exit Excel: https://stackoverflow.com/questions/45010084/unable-to-open-another-excel-file-when-one-excel-is-opened-by-net/45131821#45131821 – Richard Mneyan Jul 25 '17 at 13:53