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 SQLite with Zephyr?
- How can SQLite and ZFS be used together for software development?
- How do I use SQLite to retrieve data from a specific year?
- How do I download and install SQLite zip?
- How do I use the SQLite ZIP VFS to compress a database?
- How do I use the SQLite zfill function?
- How do I import data from a SQLite zip file?
- How can I use SQLite to query for records between two specific dates?
- How do I extract the year from a datetime value in SQLite?
- How do I call sqlitepcl.raw.setprovider() when using SQLite?
See more codes...