.NET to write data to SQLite Database

Here’s a sample code in C# using the .NET framework to write data to a SQLite database

using System;
using System.Data.SQLite;

namespace SqliteDatabaseWriterExample
{
    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 SQL statement to insert data into the SQLite database
            string sql = "INSERT INTO Customers (FirstName, LastName, Email) VALUES ('John', 'Doe', 'john.doe@example.com')";

            // 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(sql, connection))
                {
                    // Execute the SQL statement to insert data into the SQLite database
                    int rowsAffected = command.ExecuteNonQuery();

                    // Print the number of rows affected to the console
                    Console.WriteLine("{0} rows affected.", rowsAffected);
                }
            }

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

In this example, 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