从 C# 连接和使用 sqlite 数据库的最佳方法是什么?

从 C# 连接和使用 sqlite 数据库的最佳方法是什么?

问题描述:

我之前在 C++ 中通过包含 sqlite.h 完成了此操作,但在 C# 中是否有类似的简单方法?

I've done this before in C++ by including sqlite.h but is there a similarly easy way in C#?

Microsoft.DataMicrosoft 的 .Sqlite 每天有超过 9000 次下载,所以我认为您可以安全地使用它.

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

来自文档的示例用法:

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}!");
        }
    }
}