Using SQLite with C#

SQLite is a lightweight, file-based database engine that is widely used for storing data in various applications. It is known for its simplicity, speed, and reliability. In this blog post, we will explore how to use SQLite with C#.

Table of Contents

Setting up SQLite in C#

To start working with SQLite in C#, you will need to add the System.Data.SQLite NuGet package to your project. Open your project in Visual Studio, right-click on the project name in the Solution Explorer, and select “Manage NuGet Packages”. Search for System.Data.SQLite and install it.

Connecting to a SQLite Database

Once you have added the System.Data.SQLite package to your project, you can start using SQLite in your C# code. The first step is to establish a connection to a SQLite database. You can do this by creating a new instance of the SQLiteConnection class and passing the connection string as a parameter.

using System.Data.SQLite;

// Create a connection to SQLite database
SQLiteConnection connection = new SQLiteConnection("Data Source=mydatabase.db;Version=3;");

Make sure to replace mydatabase.db with the actual path to your SQLite database file.

Executing SQLite Queries

After successfully connecting to the SQLite database, you can execute various SQL queries using the SQLiteCommand class. This class represents a SQL statement or stored procedure to execute against the SQLite database. You can create a new instance of SQLiteCommand, specify the SQL query, and associate it with the SQLite connection.

using System.Data.SQLite;

// Create a command to execute a query
string sqlQuery = "INSERT INTO Customers (Name, Email) VALUES ('John Doe', 'john.doe@example.com')";
SQLiteCommand command = new SQLiteCommand(sqlQuery, connection);

// Execute the query
command.ExecuteNonQuery();

In the above example, we are executing an INSERT query to insert a new record into the Customers table.

Retrieving Data from SQLite

To retrieve data from a SQLite database, you can use the SQLiteDataReader class. This class represents a forward-only stream of rows from a query result. After executing a query, you can call the ExecuteReader method on the SQLiteCommand object to retrieve the data.

using System.Data.SQLite;

// Create a command to execute a query
string sqlQuery = "SELECT * FROM Customers";
SQLiteCommand command = new SQLiteCommand(sqlQuery, connection);

// Execute the query and get the data reader
SQLiteDataReader reader = command.ExecuteReader();

// Loop through the result set and access the data
while (reader.Read())
{
    string name = reader.GetString(0);
    string email = reader.GetString(1);
    
    Console.WriteLine($"Name: {name}, Email: {email}");
}

// Close the reader
reader.Close();

In the above example, we are executing a SELECT query to retrieve all records from the Customers table. We then loop through the result set and print the name and email of each customer.

Conclusion

Using SQLite with C# provides a lightweight and efficient way to store and retrieve data in your applications. With the help of the System.Data.SQLite package, you can easily connect to a SQLite database, execute queries, and retrieve data. SQLite is a versatile choice for small to medium-sized data storage needs, offering simplicity and reliability.

For more information, you can refer to the System.Data.SQLite documentation and the SQLite official website.

#sqlite #CSharp