简体   繁体   中英

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

我使用 - excelSheetName = row["TABLE_NAME"].ToString(); 修复了它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM