翼度科技»论坛 编程开发 .net 查看内容

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

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
 
SQLite 是一种轻量级的嵌入式数据库引擎,它在 .NET 中被广泛使用。SQLite 是一个零配置的数据库引擎,不需要服务器,可以直接在应用程序中使用。下面是一个简单的示例,演示如何在 .NET 中使用 SQLite,并提供了常见的查询、增加、修改和删除功能。
首先,你需要在项目中安装 System.Data.SQLite 包。你可以使用 NuGet 包管理器或通过 Package Manager Console 执行以下命令:
  1. Install-Package System.Data.SQLite
复制代码
接下来,创建一个 C# 文件,例如 SQLiteExample.cs,并添加以下代码:
  1. using System;
  2. using System.Data.SQLite;
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         // 指定数据库文件路径
  8.         string dbFilePath = "sample.db";
  9.         // 连接字符串
  10.         string connectionString = $"Data Source={dbFilePath};Version=3;";
  11.         // 创建数据库连接
  12.         using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  13.         {
  14.             connection.Open();
  15.             // 创建表
  16.             CreateTable(connection);
  17.             // 插入数据
  18.             InsertData(connection, "John Doe", 30);
  19.             // 查询数据
  20.             QueryData(connection);
  21.             // 更新数据
  22.             UpdateData(connection, 1, "Updated Name", 35);
  23.             // 查询更新后的数据
  24.             QueryData(connection);
  25.             // 删除数据
  26.             DeleteData(connection, 1);
  27.             // 查询删除后的数据
  28.             QueryData(connection);
  29.         }
  30.     }
  31.     static void CreateTable(SQLiteConnection connection)
  32.     {
  33.         using (SQLiteCommand command = new SQLiteCommand(
  34.             "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age INTEGER);", connection))
  35.         {
  36.             command.ExecuteNonQuery();
  37.         }
  38.         Console.WriteLine("Table created or already exists.");
  39.     }
  40.     static void InsertData(SQLiteConnection connection, string name, int age)
  41.     {
  42.         using (SQLiteCommand command = new SQLiteCommand(
  43.             "INSERT INTO Users (Name, Age) VALUES (@Name, @Age);", connection))
  44.         {
  45.             command.Parameters.AddWithValue("@Name", name);
  46.             command.Parameters.AddWithValue("@Age", age);
  47.             command.ExecuteNonQuery();
  48.         }
  49.         Console.WriteLine("Data inserted.");
  50.     }
  51.     static void QueryData(SQLiteConnection connection)
  52.     {
  53.         using (SQLiteCommand command = new SQLiteCommand(
  54.             "SELECT * FROM Users;", connection))
  55.         {
  56.             using (SQLiteDataReader reader = command.ExecuteReader())
  57.             {
  58.                 Console.WriteLine("Id\tName\tAge");
  59.                 while (reader.Read())
  60.                 {
  61.                     Console.WriteLine($"{reader["Id"]}\t{reader["Name"]}\t{reader["Age"]}");
  62.                 }
  63.             }
  64.         }
  65.         Console.WriteLine("Data queried.");
  66.     }
  67.     static void UpdateData(SQLiteConnection connection, int id, string name, int age)
  68.     {
  69.         using (SQLiteCommand command = new SQLiteCommand(
  70.             "UPDATE Users SET Name=@Name, Age=@Age WHERE Id=@Id;", connection))
  71.         {
  72.             command.Parameters.AddWithValue("@Name", name);
  73.             command.Parameters.AddWithValue("@Age", age);
  74.             command.Parameters.AddWithValue("@Id", id);
  75.             command.ExecuteNonQuery();
  76.         }
  77.         Console.WriteLine("Data updated.");
  78.     }
  79.     static void DeleteData(SQLiteConnection connection, int id)
  80.     {
  81.         using (SQLiteCommand command = new SQLiteCommand(
  82.             "DELETE FROM Users WHERE Id=@Id;", connection))
  83.         {
  84.             command.Parameters.AddWithValue("@Id", id);
  85.             command.ExecuteNonQuery();
  86.         }
  87.         Console.WriteLine("Data deleted.");
  88.     }
  89. }
复制代码
请注意,上述示例假设你已经安装了 System.Data.SQLite 包,并且在项目目录中创建了一个名为 sample.db 的 SQLite 数据库文件。在实际应用中,你需要根据自己的需求修改数据库文件路径和连接字符串。
这个示例演示了如何创建表、插入数据、查询数据、更新数据和删除数据。你可以根据具体的应用场景和需求进行修改和扩展。
 


来源:https://www.cnblogs.com/hanbing81868164/Undeclared/17963377
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具