自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

.NET中輕松應(yīng)用SQLite:零配置數(shù)據(jù)庫引擎的完美指南

數(shù)據(jù)庫 其他數(shù)據(jù)庫
下面是一個簡單的示例,演示如何在 .NET 中使用 SQLite,并提供了常見的查詢、增加、修改和刪除功能。

SQLite 是一種輕量級的嵌入式數(shù)據(jù)庫引擎,它在 .NET 中被廣泛使用。SQLite 是一個零配置的數(shù)據(jù)庫引擎,不需要服務(wù)器,可以直接在應(yīng)用程序中使用。下面是一個簡單的示例,演示如何在 .NET 中使用 SQLite,并提供了常見的查詢、增加、修改和刪除功能。

首先,你需要在項目中安裝 System.Data.SQLite 包。你可以使用 NuGet 包管理器或通過 Package Manager Console 執(zhí)行以下命令:

Install-Package System.Data.SQLite

接下來,創(chuàng)建一個 C# 文件,例如 SQLiteExample.cs,并添加以下代碼:

using System;
using System.Data.SQLite;

class Program
{
    static void Main()
    {
        // 指定數(shù)據(jù)庫文件路徑
        string dbFilePath = "sample.db";

        // 連接字符串
        string connectionString = $"Data Source={dbFilePath};Version=3;";

        // 創(chuàng)建數(shù)據(jù)庫連接
        using (SQLiteConnection connection = new SQLiteConnection(connectionString))
        {
            connection.Open();

            // 創(chuàng)建表
            CreateTable(connection);

            // 插入數(shù)據(jù)
            InsertData(connection, "John Doe", 30);

            // 查詢數(shù)據(jù)
            QueryData(connection);

            // 更新數(shù)據(jù)
            UpdateData(connection, 1, "Updated Name", 35);

            // 查詢更新后的數(shù)據(jù)
            QueryData(connection);

            // 刪除數(shù)據(jù)
            DeleteData(connection, 1);

            // 查詢刪除后的數(shù)據(jù)
            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.");
    }
}

請注意,上述示例假設(shè)你已經(jīng)安裝了 System.Data.SQLite 包,并且在項目目錄中創(chuàng)建了一個名為 sample.db 的 SQLite 數(shù)據(jù)庫文件。在實際應(yīng)用中,你需要根據(jù)自己的需求修改數(shù)據(jù)庫文件路徑和連接字符串。

這個示例演示了如何創(chuàng)建表、插入數(shù)據(jù)、查詢數(shù)據(jù)、更新數(shù)據(jù)和刪除數(shù)據(jù)。你可以根據(jù)具體的應(yīng)用場景和需求進(jìn)行修改和擴(kuò)展。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2023-11-08 08:32:16

2013-04-01 10:49:51

iOS開發(fā)sqlite數(shù)據(jù)庫

2010-01-27 18:33:16

Android SQL

2013-03-27 09:47:01

Android開發(fā)SQAndroid SDK

2011-03-04 11:08:46

ADO.NET數(shù)據(jù)庫

2011-08-30 14:15:34

QTSQLite數(shù)據(jù)庫

2011-07-27 10:16:41

iPhone SQLite 數(shù)據(jù)庫

2012-06-04 13:16:39

Ubuntu數(shù)據(jù)庫

2009-07-28 17:49:44

ASP.NET數(shù)據(jù)庫連

2024-01-16 09:35:00

數(shù)據(jù)庫應(yīng)用

2023-12-13 11:23:15

2024-09-20 18:02:42

C#數(shù)據(jù)庫SQLite

2011-04-11 13:09:56

數(shù)據(jù)庫

2010-06-11 09:04:30

MySQL數(shù)據(jù)庫

2011-07-20 12:34:49

SQLite數(shù)據(jù)庫約束

2019-08-15 07:00:54

SQLite數(shù)據(jù)庫內(nèi)存數(shù)據(jù)庫

2017-07-12 09:20:42

SQLite數(shù)據(jù)庫移植

2025-01-10 00:32:48

2024-11-25 06:30:00

2011-08-24 13:49:45

Access數(shù)據(jù)庫轉(zhuǎn)化
點贊
收藏

51CTO技術(shù)棧公眾號