C#中引用SQLite的使用
    		       		warning:
    		            这篇文章距离上次修改已过420天,其中的内容可能已经有所变动。
    		        
        		                
                在C#中使用SQLite,首先需要安装System.Data.SQLite库。可以通过NuGet包管理器进行安装。
以下是一个简单的示例,展示了如何在C#中使用SQLite:
using System;
using System.Data.SQLite;
 
namespace SQLiteExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "Data Source=example.db;Version=3;";
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
 
                using (var command = new SQLiteCommand(connection))
                {
                    command.CommandText = "CREATE TABLE IF NOT EXISTS People (Id INTEGER PRIMARY KEY, Name TEXT)";
                    command.ExecuteNonQuery();
 
                    command.CommandText = "INSERT INTO People (Name) VALUES ('John Doe')";
                    command.ExecuteNonQuery();
 
                    command.CommandText = "SELECT * FROM People";
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Console.WriteLine($"ID: {reader["Id"]}, Name: {reader["Name"]}");
                        }
                    }
                }
            }
        }
    }
}在这个例子中,我们创建了一个名为example.db的SQLite数据库,创建了一个名为People的表,插入了一条记录,并且从表中查询出记录并打印出来。
确保在运行此代码之前,你的项目中已经安装了System.Data.SQLite库。如果没有安装,可以通过NuGet包管理器搜索并安装。
评论已关闭