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."); } }