How to Connect and Use a SQLite Database from C#

What is the best way to connect and use a sqlite database from C#

Microsoft.Data.Sqlite by Microsoft has over 9000 downloads every day, so I think you are safe using that one.

Example usage from the documentation:

using (var connection = new SqliteConnection("Data Source=hello.db"))
{
connection.Open();

var command = connection.CreateCommand();
command.CommandText =
@"
SELECT name
FROM user
WHERE id = $id
";
command.Parameters.AddWithValue("$id", id);

using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var name = reader.GetString(0);

Console.WriteLine($"Hello, {name}!");
}
}
}

How can I access SQLite with C#?

You can not connect to sqlite db using SQLProvider classes. They are for sql server. You need to use SQLite provider classes.



Related Topics



Leave a reply



Submit