Exceldatareader reference issues

Dinesh Kalva 100 Reputation points
2023-08-13T04:30:29.3+00:00

Hi, I'm getting an error when I run package. I want to read huge excel data and getting an error at below "using...." When i comment, am not getting an error.

When I have added these references exceldatareader and exceldatareader.dataset from nugget in script task in solution explorer and close it, it is automatically losing the references and can see Yellow mark at references in script task.

Also, I want to add filename, sheetname to the table.

using (var reader = ExcelReaderFactory.CreateReader(stream))      
using ExcelDataReader; 
using System; 
using System.Data; 
using System.Data.SqlClient; using System.IO;  

public Main()     
{         
string excelFilePath = "path_to_your_excel_file.xlsx";         
string connectionString = "your_sql_server_connection_string";         
string tableName = "YourDestinationTable";          
// Read Excel data into a DataSet using ExcelDataReader         
using (var stream = File.Open(excelFilePath, FileMode.Open, FileAccess.Read))         
{             
using (var reader = ExcelReaderFactory.CreateReader(stream))             
{                 
var dataSet = reader.AsDataSet();                  
// Get the desired DataTable from the DataSet (assuming it's the first table)                 DataTable dataTable = dataSet.Tables[0];                  
// Bulk insert the DataTable into SQL Server using SqlBulkCopy                 
using (SqlConnection connection = new SqlConnection(connectionString))                
 {                   
  connection.Open();                      
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))                     
{                        
bulkCopy.DestinationTableName = tableName;                         bulkCopy.WriteToServer(dataTable);                    
 }                 
}             
}         
}          
Console.WriteLine("Data loaded and inserted into SQL Server.");     } }
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,702 questions
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-08-13T08:21:53.6166667+00:00

    In regards to automatically losing the references and can see Yellow mark there will be a message indicating the package was rolled back because the package is not compatible with your project's Framework. And with that said, if you look at the supported frameworks ExcelDataReader pretty much covers all the .NET Frameworks back to .NET 4.6 and covers .NET Core as per this page.

    Make sure you are installing a non-preview version of ExcelDataReader.

    In regards to and getting an error at below "using...." When i comment, am not getting an error., same as above.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.