Venkat
Venkat

Reputation: 89

Read excel file using oledb in Script task

I am trying to read data from Excel using the oledb command in the script task of SSIS. The excel does not have sheet names. I am getting error as the sheet name is empty. Below is the code used

excelConnection = new OleDbConnection(excelConnectionString);
                excelConnection.Open();  

                DataTable excelDataTable = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string excelSheetName = string.Empty;

                foreach (DataRow row in excelDataTable.Rows)
                {
                    excelSheetName = row[0].ToString().Trim("'".ToCharArray());
                    //Console.writeLine(excelSheetName);
                }

                OleDbDataAdapter excelAdapter = new OleDbDataAdapter();
                OleDbCommand excelCommand = new OleDbCommand();
                DataSet excelDataSet = new DataSet();

                excelCommand.Connection = excelConnection;
                excelCommand.CommandText = "Select * from [" + excelSheetName + "]";
                excelAdapter.SelectCommand = excelCommand;

                excelAdapter.Fill(excelDataSet);  // error here

How to fix this? Thanks

Upvotes: 1

Views: 246

Answers (1)

Venkat
Venkat

Reputation: 89

I fixed it using - excelSheetName = row["TABLE_NAME"].ToString();

Upvotes: 1

Related Questions