Monday, July 11, 2022

Read Excel Data From Oracle Database

 using System.Data.OleDb;

protected DataSet ReadExcel()

        {

            // Create a DataSet which will hold the data extracted from the worksheet.

            DataSet ds = new DataSet();


            string xls = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\murdhusi\Desktop\Mails\Data_29062022.xlsx;Extended Properties=Excel 8.0";

            string xlsx = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\murdhusi\Desktop\Mails\Data_29062022.xlsx;Extended Properties=Excel 12.0";


            string connString = xlsx;

            // Create the connection object

            OleDbConnection oledbConn = new OleDbConnection(connString);

            try

            {

                // Open connection

                oledbConn.Open();


                // Create OleDbCommand object and select data from worksheet Sheet1

                OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Export Worksheet$]", oledbConn);


                // Create new OleDbDataAdapter

                OleDbDataAdapter oleda = new OleDbDataAdapter();


                oleda.SelectCommand = cmd;




                // Fill the DataSet from the data extracted from the worksheet.

                oleda.Fill(ds, "Employees");


                return ds;

            }

            catch

            {

            }

            finally

            {

                // Close connection

                oledbConn.Close();

            }

            return ds;

        }