偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

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

Install-Package System.Data.SQLite

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

using System;
using System.Data.SQLite;

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

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

        // 創(chuàng)建數(shù)據(jù)庫(kù)連接
        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.");
    }
}

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

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

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

2023-11-08 08:32:16

2013-04-01 10:49:51

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

2010-01-27 18:33:16

Android SQL

2011-03-04 11:08:46

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

2013-03-27 09:47:01

Android開(kāi)發(fā)SQAndroid SDK

2012-06-04 13:16:39

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

2011-07-27 10:16:41

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

2011-08-30 14:15:34

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

2023-12-13 11:23:15

2024-01-16 09:35:00

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

2009-07-28 17:49:44

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

2011-04-11 13:09:56

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

2010-06-11 09:04:30

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

2024-09-20 18:02:42

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

2024-11-25 06:30:00

2011-07-20 12:34:49

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

2019-08-15 07:00:54

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

2017-07-12 09:20:42

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

2025-01-10 00:32:48

2023-11-02 10:32:27

GoGORM
點(diǎn)贊
收藏

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