.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("Id\tName\tAge");
                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 数据库文件。在实际应用中,你需要根据自己的需求修改数据库文件路径和连接字符串。

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

责任编辑:姜华 来源: 今日头条
相关推荐

2023-11-08 08:32:16

2013-04-01 10:49:51

iOS开发sqlite数据库

2010-01-27 18:33:16

Android SQL

2013-03-27 09:47:01

Android开发SQAndroid SDK

2012-06-04 13:16:39

Ubuntu数据库

2011-08-30 14:15:34

QTSQLite数据库

2011-07-27 10:16:41

iPhone SQLite 数据库

2011-03-04 11:08:46

ADO.NET数据库

2024-01-16 09:35:00

数据库应用

2009-07-28 17:49:44

ASP.NET数据库连

2023-12-13 11:23:15

2011-07-20 12:34:49

SQLite数据库约束

2019-08-15 07:00:54

SQLite数据库内存数据库

2017-07-12 09:20:42

SQLite数据库移植

2011-04-11 13:09:56

数据库

2010-06-11 09:04:30

MySQL数据库

2011-08-24 13:49:45

Access数据库转化

2023-11-02 10:32:27

GoGORM

2010-05-19 18:11:05

2019-11-06 09:23:20

数据库配置网络
点赞
收藏

51CTO技术栈公众号