sqliteHow can I use SQLite in a C# project?
SQLite is a lightweight database that can be used in C# projects. It is a self-contained, serverless, zero-configuration, transactional SQL database engine. To use SQLite in a C# project, you need to install the NuGet package for the System.Data.SQLite library.
Once the NuGet package is installed, you can use the following example code to create a database and table:
using System;
using System.Data.SQLite;
namespace SQLiteExample
{
class Program
{
static void Main(string[] args)
{
// Create a connection to the database
SQLiteConnection connection = new SQLiteConnection("Data Source=database.db;Version=3;");
connection.Open();
// Create a table
string sql = "CREATE TABLE IF NOT EXISTS Highscores (name VARCHAR(20), score INT)";
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
// Close connection
connection.Close();
}
}
}
This code will create a database file called database.db
and a table called Highscores
with two columns (name
and score
).
The code consists of the following parts:
using System;
andusing System.Data.SQLite;
- These lines import the necessary libraries.SQLiteConnection connection = new SQLiteConnection("Data Source=database.db;Version=3;");
- This creates a connection to the SQLite database.string sql = "CREATE TABLE IF NOT EXISTS Highscores (name VARCHAR(20), score INT)";
- This creates a SQL command to create theHighscores
table.SQLiteCommand command = new SQLiteCommand(sql, connection);
- This creates a command object with the SQL command and connection.command.ExecuteNonQuery();
- This executes the SQL command.connection.Close();
- This closes the connection to the database.
For more information, see the following links:
More of Sqlite
- How do I use the SQLite ZIP VFS to compress a database?
- How do I use SQLite with Visual Studio?
- How can I use an upsert statement to update data in a SQLite database?
- How do I use UUIDs in SQLite?
- How do I use the SQLite sequence feature?
- How to configure SQLite with XAMPP on Windows?
- How do I create a view in SQLite?
- How do I use the SQLite SUBSTRING function?
- How to use SQLite's strftime function?
- How do I show the tables in a SQLite database?
See more codes...