.NET中轻松应用SQLite:零配置数据库引擎的完美指南

  • .NET中轻松应用SQLite:零配置数据库引擎的完美指南已关闭评论
  • 75 次浏览
  • A+
所属分类:.NET技术
摘要

 SQLite 是一种轻量级的嵌入式数据库引擎,它在 .NET 中被广泛使用。SQLite 是一个零配置的数据库引擎,不需要服务器,可以直接在应用程序中使用。下面是一个简单的示例,演示如何在 .NET 中使用 SQLite,并提供了常见的查询、增加、修改和删除功能。

.NET中轻松应用SQLite:零配置数据库引擎的完美指南

 

SQLite 是一种轻量级的嵌入式数据库引擎,它在 .NET 中被广泛使用。SQLite 是一个零配置的数据库引擎,不需要服务器,可以直接在应用程序中使用。下面是一个简单的示例,演示如何在 .NET 中使用 SQLite,并提供了常见的查询、增加、修改和删除功能。

首先,你需要在项目中安装 System.Data.SQLite 包。你可以使用 NuGet 包管理器或通过 Package Manager Console 执行以下命令:

Install-Package System.Data.SQLite

接下来,创建一个 C# 文件,例如 SQLiteExample.cs,并添加以下代码:

using System; using System.Data.SQLite;  class Program {     static void Main()     {         // 指定数据库文件路径         string dbFilePath = "sample.db";          // 连接字符串         string connectionString = $"Data Source={dbFilePath};Version=3;";          // 创建数据库连接         using (SQLiteConnection connection = new SQLiteConnection(connectionString))         {             connection.Open();              // 创建表             CreateTable(connection);              // 插入数据             InsertData(connection, "John Doe", 30);              // 查询数据             QueryData(connection);              // 更新数据             UpdateData(connection, 1, "Updated Name", 35);              // 查询更新后的数据             QueryData(connection);              // 删除数据             DeleteData(connection, 1);              // 查询删除后的数据             QueryData(connection);         }     }      static void CreateTable(SQLiteConnection connection)     {         using (SQLiteCommand command = new SQLiteCommand(             "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age INTEGER);", connection))         {             command.ExecuteNonQuery();         }          Console.WriteLine("Table created or already exists.");     }      static void InsertData(SQLiteConnection connection, string name, int age)     {         using (SQLiteCommand command = new SQLiteCommand(             "INSERT INTO Users (Name, Age) VALUES (@Name, @Age);", connection))         {             command.Parameters.AddWithValue("@Name", name);             command.Parameters.AddWithValue("@Age", age);              command.ExecuteNonQuery();         }          Console.WriteLine("Data inserted.");     }      static void QueryData(SQLiteConnection connection)     {         using (SQLiteCommand command = new SQLiteCommand(             "SELECT * FROM Users;", connection))         {             using (SQLiteDataReader reader = command.ExecuteReader())             {                 Console.WriteLine("IdtNametAge");                 while (reader.Read())                 {                     Console.WriteLine($"{reader["Id"]}t{reader["Name"]}t{reader["Age"]}");                 }             }         }         Console.WriteLine("Data queried.");     }      static void UpdateData(SQLiteConnection connection, int id, string name, int age)     {         using (SQLiteCommand command = new SQLiteCommand(             "UPDATE Users SET Name=@Name, Age=@Age WHERE Id=@Id;", connection))         {             command.Parameters.AddWithValue("@Name", name);             command.Parameters.AddWithValue("@Age", age);             command.Parameters.AddWithValue("@Id", id);              command.ExecuteNonQuery();         }          Console.WriteLine("Data updated.");     }      static void DeleteData(SQLiteConnection connection, int id)     {         using (SQLiteCommand command = new SQLiteCommand(             "DELETE FROM Users WHERE Id=@Id;", connection))         {             command.Parameters.AddWithValue("@Id", id);              command.ExecuteNonQuery();         }          Console.WriteLine("Data deleted.");     } }

请注意,上述示例假设你已经安装了 System.Data.SQLite 包,并且在项目目录中创建了一个名为 sample.db 的 SQLite 数据库文件。在实际应用中,你需要根据自己的需求修改数据库文件路径和连接字符串。

这个示例演示了如何创建表、插入数据、查询数据、更新数据和删除数据。你可以根据具体的应用场景和需求进行修改和扩展。

 

.NET中轻松应用SQLite:零配置数据库引擎的完美指南