从 C# 连接和使用 sqlite 数据库的最佳方法是什么? [英] What is the best way to connect and use a sqlite database from C#

查看:25
本文介绍了从 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}!");
        }
    }
}

这篇关于从 C# 连接和使用 sqlite 数据库的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆