在.NET中使用SQLite,你可以使用官方的System.Data.SQLite库,或者使用更现代的Microsoft.Data.Sqlite库。以下是使用Microsoft.Data.Sqlite的一个基本示例。
首先,确保安装了Microsoft.Data.Sqlite NuGet包。
dotnet add package Microsoft.Data.Sqlite然后,你可以使用以下代码来连接SQLite数据库并执行查询:
using Microsoft.Data.Sqlite;
using System;
using System.Data.Common;
 
namespace SQLiteExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 连接字符串指向SQLite数据库文件
            string connectionString = "Data Source=example.db";
 
            // 使用using语句确保资源被正确释放
            using (var connection = new SqliteConnection(connectionString))
            {
                connection.Open();
 
                // 创建一个SQL命令
                string sql = "SELECT 1";
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = sql;
 
                    // 使用ExecuteScalar来执行命令并获取结果
                    var result = command.ExecuteScalar();
                    Console.WriteLine(result);
                }
            }
        }
    }
}这个示例展示了如何在.NET中打开一个到SQLite数据库的连接,执行一个查询并输出结果。记得替换example.db为你的数据库文件路径。如果数据库文件不存在,SQLite将创建它。