Modern ADO .Net with SQLite in VB and C#

Modern ADO .Net with SQLite DB connect with VB and C# with example code will be discussed here. In .net core environment like 5/6/7 ADO .net provider for SQLite Microsoft.Data.Sqlite lightweight package.

To install the Microsoft. Data. SQLite Package runs the below command in the Package Manager Console.

Install-Package Microsoft.Data.Sqlite

SQLite with VB Example code.

Imports System
Imports Microsoft.Data.Sqlite

Module Module1
    Sub Main()
        Dim connectionString As String = "Data Source=mydatabase.db"

        Using connection As New SqliteConnection(connectionString)
            connection.Open()

            ' Create a table
            Dim createTableQuery As String = "CREATE TABLE IF NOT EXISTS SampleTable (Id INTEGER PRIMARY KEY, Name TEXT)"
            Using createCommand As New SqliteCommand(createTableQuery, connection)
                createCommand.ExecuteNonQuery()
            End Using

            ' Insert a record
            Dim insertQuery As String = "INSERT INTO SampleTable (Name) VALUES ('John Doe')"
            Using insertCommand As New SqliteCommand(insertQuery, connection)
                insertCommand.ExecuteNonQuery()
            End Using

            ' Read records
            Dim selectQuery As String = "SELECT * FROM SampleTable"
            Using selectCommand As New SqliteCommand(selectQuery, connection)
                Using reader As SqliteDataReader = selectCommand.ExecuteReader()
                    While reader.Read()
                        Console.WriteLine($"Id: {reader("Id")}, Name: {reader("Name")}")
                    End While
                End Using
            End Using
        End Using
    End Sub
End Module

SQLite DB with c# example.

using System;
using Microsoft.Data.Sqlite;
class Program
{
    static void Main()
    {
        string connectionString = "Data Source=mydatabase.db";
        using (var connection = new SqliteConnection(connectionString))
        {
            connection.Open();

            // Create table query
            string createTableQuery = "CREATE TABLE IF NOT EXISTS SampleTable (Id INTEGER PRIMARY KEY, Name TEXT)";
            using (var createCommand = new SqliteCommand(createTableQuery, connection))
            {
                createCommand.ExecuteNonQuery();
            }
            // Insert data query
            string insertQuery = "INSERT INTO SampleTable (Name) VALUES ('John Doe')";
            using (var insertCommand = new SqliteCommand(insertQuery, connection))
            {
                insertCommand.ExecuteNonQuery();
            }
            // Select data query
            string selectQuery = "SELECT * FROM SampleTable";
            using (var selectCommand = new SqliteCommand(selectQuery, connection))
            using (var reader = selectCommand.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine($"Id: {reader["Id"]}, Name: {reader["Name"]}");
                }
            }
        }
    }
}

Output

Output