.NET code to load data from CSV to SQLite database

Here’s a sample code in C# using the .NET framework to write data from a CSV file to a SQLite Database

using System;
using System.Data.SQLite;
using System.IO;

namespace SqliteDatabaseCsvWriterExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define the connection string for the SQLite database
            string connectionString = "Data Source=database.db;Version=3;";

            // Define the path to the CSV file
            string csvFilePath = "customers.csv";

            // Read the CSV file and insert the data into the SQLite database
            using (StreamReader reader = new StreamReader(csvFilePath))
            {
                // Create a new instance of the SQLiteConnection class
                using (SQLiteConnection connection = new SQLiteConnection(connectionString))
                {
                    // Open the connection to the SQLite database
                    connection.Open();

                    // Create a new instance of the SQLiteCommand class
                    using (SQLiteCommand command = new SQLiteCommand(connection))
                    {
                        // Read the CSV file line by line
                        while (!reader.EndOfStream)
                        {
                            // Parse the values from the current line of the CSV file
                            string line = reader.ReadLine();
                            string[] values = line.Split(',');

                            // Define the SQL statement to insert data into the SQLite database
                            string sql = "INSERT INTO Customers (FirstName, LastName, Email) VALUES ('" + values[0] + "', '" + values[1] + "', '" + values[2] + "')";

                            // Set the SQL statement of the SQLiteCommand object
                            command.CommandText = sql;

                            // Execute the SQL statement to insert data into the SQLite database
                            int rowsAffected = command.ExecuteNonQuery();
                        }
                    }
                }
            }

            Console.WriteLine("Data has been written to the SQLite database.");
            Console.ReadLine();
        }
    }
}

In this example, the StreamReader class is used to read the CSV file line by line. The values from each line of the CSV file are parsed using the Split method to split the line into an array of values. The SQLiteConnection class is used to create a new instance of the SQLite connection to the SQLite database. The Open method of the SQLiteConnection object is used to open the connection. The SQLiteCommand class is used to create a new instance of the SQL command to insert data into the SQLite database. The ExecuteNonQuery method of the SQLiteCommand object is used to execute the SQL statement and insert data into the SQLite database. Finally, the number of rows affected is printed to the console.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2025 Pamai Tech