Reading data from CSV (Comma Separated Values) files and inserting it into a SQL Server database is a routine requirement for many developers—whether you're working on reporting, migrations, data ingestion scripts, or admin dashboards. In C#, this can be implemented efficiently using the IronXL library, which makes reading and parsing spreadsheet data seamless, and Microsoft.Data.SqlClient to interact securely with SQL Server.
In this article, we’ll walk through a complete example of importing CSV data into a SQL Server database using IronXL. You don’t need Microsoft Excel installed, and the entire process—from reading the file to writing the data—is handled programmatically and efficiently.
How to Read data from csv file and store it in database C
Install Required Libraries via NuGet
Set Up License Key and Namespaces
Read the CSV File using WorkBook.Load method
Convert into a DataTable using sheet.ToDataTable(true)
Connect to SQL Server Database using Connection string
Insert data into Database table using using SqlBulkCopy
Introduction to IronXL
IronXL is a robust and developer-friendly .NET library used to read, write, and edit Excel and CSV files in C# without needing Excel installed. It simplifies file parsing and supports converting spreadsheet contents into formats that are easy to process in code—like DataTable, List, or JSON.
Whether you are working with .xlsx, .xls, or .csv files, IronXL lets you manipulate the data directly and without the hassle of Office Interop or third-party dependencies. This makes it ideal for data imports, automation workflows, or business tools built on .NET.
IronXL is especially effective when you're handling structured data like CSVs, as it loads CSV files into memory as Excel-compatible worksheets—making further processing extremely intuitive.
Features of IronXL
Works without Microsoft Excel or Office installed
Supports .csv, .xlsx, .xls, .tsv formats
Read and write spreadsheet data with minimal code
Convert worksheets to DataTable, JSON, arrays, and more
Fully compatible with .NET Framework, .NET Core, .NET 6/7+
Works across Windows, Linux, and cloud environments
How to Read CSV File and Insert it into SQL Server Table
Let’s now walk through the process step by step:
Step 1: Install Required Libraries via NuGet
We'll need two libraries: IronXL.Excel and Microsoft.Data.SqlClient.
Option 1: Visual Studio GUI
Right-click your project → Manage NuGet Packages
Search for IronXL.Excel and click Install
- Do the same for Microsoft.Data.SqlClient
Option 2: Package Manager Console
Install-Package IronXL.Excel
Install-Package Microsoft.Data.SqlClient
Option 3: .NET CLI
dotnet add package IronXL.Excel
dotnet add package Microsoft.Data.SqlClient
Step 2: Set Up License Key and Namespaces
Set the IronXL license key via environment variable for security:
License.LicenseKey = Environment.GetEnvironmentVariable("IronSuite", EnvironmentVariableTarget.Machine);
Add the required using statements:
using IronXL;
using Microsoft.Data.SqlClient;
using System.Data;
Step 3: Prepare the CSV File
Create a file named employees_data.csv and place it in your project directory with the following content:
EmployeeID,Name,Department,JoiningDate
26,Employee 1,Department 2,6/14/2025
27,Employee 2,Department 3,5/30/2025
28,Employee 3,Department 4,5/15/2025
29,Employee 4,Department 5,4/30/2025
30,Employee 5,Department 1,4/15/2025
31,Employee 6,Department 2,3/31/2025
32,Employee 7,Department 3,3/16/2025
33,Employee 8,Department 4,3/1/2025
34,Employee 9,Department 5,2/14/2025
35,Employee 10,Department 1,1/30/2025
36,Employee 11,Department 2,1/15/2025
37,Employee 12,Department 3,12/31/2024
38,Employee 13,Department 4,12/16/2024
39,Employee 14,Department 5,12/1/2024
40,Employee 15,Department 1,11/16/2024
41,Employee 16,Department 2,11/1/2024
42,Employee 17,Department 3,10/17/2024
43,Employee 18,Department 4,10/2/2024
44,Employee 19,Department 5,9/17/2024
45,Employee 20,Department 1,9/2/2024
46,Employee 21,Department 2,8/18/2024
47,Employee 22,Department 3,8/3/2024
48,Employee 23,Department 4,7/19/2024
49,Employee 24,Department 5,7/4/2024
50,Employee 25,Department 1,6/19/2024
Each row represents a new employee entry. This will be our input file.
Step 4: Create the SQL Table
Using SQL Server Management Studio (SSMS), create a table to hold the data:
CREATE TABLE Employees (
Name NVARCHAR(100),
Department NVARCHAR(100),
JoiningDate date
);
Ensure this is created in a database like TestDB.
Step 5: Read the CSV File into a DataTable
IronXL loads CSV files as if they were Excel files. You can directly convert the sheet into a DataTable.
DataTable ReadCSV(string filePath)
{
WorkBook workbook = WorkBook.Load(filePath);
WorkSheet sheet = workbook.DefaultWorkSheet;
DataTable dataTable = sheet.ToDataTable(true); // 'true' if headers exist
return dataTable;
}
This reads the CSV and gives you a ready-to-use DataTable with rows and columns.
Step 6: Connect to the SQL Server
We’ll create a simple helper method to connect to the database.
string connectionString = @"Server=localhost;Database=TestDB;Trusted_Connection=True;TrustServerCertificate=True;";
SqlConnection ConnectToDatabase()
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
return connection;
}
Step 7: Create the Table (If Not Exists) and Insert Data
This method will create the table dynamically based on the DataTable schema (if it doesn’t already exist) and insert all rows using SqlBulkCopy.
void ImportData(DataTable dataTable)
{
using (SqlConnection connection = ConnectToDatabase())
{
string tableName = "Employees";
// Create table dynamically if it doesn't exist
string checkTable = $"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{tableName}') BEGIN ";
string createTable = "CREATE TABLE " + tableName + " (";
for (int i = 0; i < dataTable.Columns.Count; i++)
{
createTable += $"[{dataTable.Columns[i].ColumnName}] NVARCHAR(MAX)";
if (i < dataTable.Columns.Count - 1)
createTable += ", ";
}
createTable += ") END";
SqlCommand createTableCommand = new SqlCommand(checkTable + createTable, connection);
createTableCommand.ExecuteNonQuery();
// Insert data using SqlBulkCopy
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.DestinationTableName = tableName;
try
{
bulkCopy.WriteToServer(dataTable);
Console.WriteLine("Data imported successfully!");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
This block ensures the table matches the CSV schema and performs a fast, batched insert of all rows.
Step 8: Run Everything
Now just tie it together:
License.LicenseKey = Environment.GetEnvironmentVariable("IronSuite", EnvironmentVariableTarget.Machine);
string filePath = "employees_data.csv";
DataTable dataTable = ReadCSV(filePath);
ImportData(dataTable);
If everything works well, you'll see data imported successfully!
Step 9: Verify the Imported Data
Open SQL Server Management Studio and run:
SELECT * FROM Employees;
You should see:
Licensing Information
IronXL offers flexible licensing depending on your project type:
Free Trial – Fully functional for development and evaluation
Developer License – For individuals and single apps
Project & Site Licenses – For commercial use
Redistribution License – For SaaS platforms or OEM products
For pricing and full terms, visit the IronXL Licensing Page.
Conclusion
Reading a CSV file and inserting the data into SQL Server doesn’t have to be complicated. With IronXL, the process becomes clean, efficient, and production-ready—with no need for external tools like Excel Interop or manual CSV parsing.
If you’re building import tools, dashboards, or automated jobs, IronXL is an excellent choice to streamline spreadsheet handling in .NET.
Get started with the IronXL Free Trial and simplify your data workflows today.
Top comments (0)