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

在 C# 中調(diào)用 SQLite 數(shù)據(jù)庫(kù)

數(shù)據(jù)庫(kù)
在C#中,通過(guò)SQLite數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)存儲(chǔ)和檢索是一項(xiàng)常見(jiàn)任務(wù)。本文將介紹如何在C#中使用SQLite數(shù)據(jù)庫(kù),并提供相應(yīng)的示例代碼。

SQLite 是一種輕量級(jí)的嵌入式關(guān)系型數(shù)據(jù)庫(kù),廣泛用于移動(dòng)應(yīng)用、桌面應(yīng)用以及嵌入式系統(tǒng)中。在C#中,通過(guò)SQLite數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)存儲(chǔ)和檢索是一項(xiàng)常見(jiàn)任務(wù)。本文將介紹如何在C#中使用SQLite數(shù)據(jù)庫(kù),并提供相應(yīng)的示例代碼。

準(zhǔn)備工作

  • 安裝SQLite庫(kù): 在C#項(xiàng)目中,我們通常使用System.Data.SQLite庫(kù)來(lái)與SQLite數(shù)據(jù)庫(kù)進(jìn)行交互。你可以通過(guò)NuGet包管理器來(lái)安裝這個(gè)庫(kù)。常用的包有System.Data.SQLite和Microsoft.Data.Sqlite。本文將使用Microsoft.Data.Sqlite。在Visual Studio中,右鍵點(diǎn)擊你的項(xiàng)目,選擇“管理NuGet包”,然后搜索Microsoft.Data.Sqlite并安裝。
  • 創(chuàng)建SQLite數(shù)據(jù)庫(kù): 如果你還沒(méi)有SQLite數(shù)據(jù)庫(kù),可以使用SQLite工具(如DB Browser for SQLite)創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)文件,或者在代碼中動(dòng)態(tài)創(chuàng)建。

示例代碼

下面是一個(gè)完整的示例,展示如何在C#中使用SQLite數(shù)據(jù)庫(kù)進(jìn)行創(chuàng)建表、插入數(shù)據(jù)、查詢數(shù)據(jù)和更新數(shù)據(jù)等操作。

1. 創(chuàng)建SQLite連接

首先,我們需要?jiǎng)?chuàng)建一個(gè)SQLite連接。

using System;
using Microsoft.Data.Sqlite;

class Program
{
    private static string connectionString = "Data Source=mydatabase.db";

    static void Main(string[] args)
    {
        using (var connection = new SqliteConnection(connectionString))
        {
            connection.Open();

            // 在這里進(jìn)行數(shù)據(jù)庫(kù)操作
            CreateTable(connection);
            InsertData(connection);
            QueryData(connection);
            UpdateData(connection);
        }
    }

    // 其他數(shù)據(jù)庫(kù)操作方法...
}

2. 創(chuàng)建表

接下來(lái),我們創(chuàng)建一個(gè)表。例如,我們創(chuàng)建一個(gè)名為Users的表,包含Id和Name兩個(gè)字段。

static void CreateTable(SqliteConnection connection)
{
    string sql = "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY, Name TEXT)";

    using (var command = new SqliteCommand(sql, connection))
    {
        command.ExecuteNonQuery();
        Console.WriteLine("Table 'Users' created.");
    }
}

3. 插入數(shù)據(jù)

向Users表中插入一些數(shù)據(jù)。

static void InsertData(SqliteConnection connection)
{
    string sql = "INSERT INTO Users (Name) VALUES (@Name)";

    using (var command = new SqliteCommand(sql, connection))
    {
        command.Parameters.AddWithValue("@Name", "Alice");
        command.ExecuteNonQuery();

        command.Parameters.AddWithValue("@Name", "Bob");
        command.ExecuteNonQuery();

        Console.WriteLine("Data inserted into 'Users'.");
    }
}

4. 查詢數(shù)據(jù)

從Users表中查詢數(shù)據(jù)并輸出。

static void QueryData(SqliteConnection connection)
{
    string sql = "SELECT Id, Name FROM Users";

    using (var command = new SqliteCommand(sql, connection))
    {
        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine($"Id: {reader["Id"]}, Name: {reader["Name"]}");
            }
        }
    }
}

5. 更新數(shù)據(jù)

更新Users表中的數(shù)據(jù)。

static void UpdateData(SqliteConnection connection)
{
    string sql = "UPDATE Users SET Name = @NewName WHERE Id = @Id";

    using (var command = new SqliteCommand(sql, connection))
    {
        command.Parameters.AddWithValue("@NewName", "Charlie");
        command.Parameters.AddWithValue("@Id", 1);
        command.ExecuteNonQuery();

        Console.WriteLine("Data updated in 'Users'.");
    }
}

完整代碼

以下是完整代碼的匯總:

using System;
using Microsoft.Data.Sqlite;

class Program
{
    private static string connectionString = "Data Source=mydatabase.db";

    static void Main(string[] args)
    {
        using (var connection = new SqliteConnection(connectionString))
        {
            connection.Open();

            CreateTable(connection);
            InsertData(connection);
            QueryData(connection);
            UpdateData(connection);
            QueryData(connection);  // 再次查詢以查看更新結(jié)果
        }
    }

    static void CreateTable(SqliteConnection connection)
    {
        string sql = "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY, Name TEXT)";

        using (var command = new SqliteCommand(sql, connection))
        {
            command.ExecuteNonQuery();
            Console.WriteLine("Table 'Users' created.");
        }
    }

    static void InsertData(SqliteConnection connection)
    {
        string sql = "INSERT INTO Users (Name) VALUES (@Name)";

        using (var command = new SqliteCommand(sql, connection))
        {
            command.Parameters.AddWithValue("@Name", "Alice");
            command.ExecuteNonQuery();

            command.Parameters.AddWithValue("@Name", "Bob");
            command.ExecuteNonQuery();

            Console.WriteLine("Data inserted into 'Users'.");
        }
    }

    static void QueryData(SqliteConnection connection)
    {
        string sql = "SELECT Id, Name FROM Users";

        using (var command = new SqliteCommand(sql, connection))
        {
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine($"Id: {reader["Id"]}, Name: {reader["Name"]}");
                }
            }
        }
    }

    static void UpdateData(SqliteConnection connection)
    {
        string sql = "UPDATE Users SET Name = @NewName WHERE Id = @Id";

        using (var command = new SqliteCommand(sql, connection))
        {
            command.Parameters.AddWithValue("@NewName", "Charlie");
            command.Parameters.AddWithValue("@Id", 1);
            command.ExecuteNonQuery();

            Console.WriteLine("Data updated in 'Users'.");
        }
    }
}

結(jié)論

通過(guò)本文,我們了解了如何在C#中使用SQLite數(shù)據(jù)庫(kù)進(jìn)行基本的CRUD操作。使用Microsoft.Data.Sqlite庫(kù),可以方便地創(chuàng)建連接、執(zhí)行SQL命令以及查詢和處理結(jié)果。SQLite的輕量級(jí)特性使得它非常適合嵌入式和本地存儲(chǔ)的應(yīng)用場(chǎng)景。希望本文對(duì)你有所幫助,歡迎你在項(xiàng)目中嘗試使用SQLite數(shù)據(jù)庫(kù)!

責(zé)任編輯:趙寧寧 來(lái)源: 后端Q
相關(guān)推薦

2009-08-24 18:09:13

C#調(diào)用Oracle數(shù)

2024-02-28 08:06:17

2009-08-11 14:26:56

C#動(dòng)態(tài)調(diào)用WebSe

2011-03-17 15:59:37

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

2010-12-20 09:44:36

SQLite.C#

2024-04-18 09:56:16

2009-08-05 16:49:42

C#中調(diào)用dll

2009-08-11 13:35:13

C# Berkeley

2013-04-01 10:49:51

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

2011-08-30 14:15:34

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

2009-08-11 14:51:47

C#讀取Excel中數(shù)

2009-03-19 10:08:09

C#數(shù)據(jù)庫(kù)查詢

2010-10-26 15:21:11

連接Oracle數(shù)據(jù)庫(kù)

2009-08-06 18:10:06

C#數(shù)據(jù)庫(kù)事務(wù)

2009-08-17 17:42:57

C#數(shù)據(jù)庫(kù)操作類

2009-08-25 14:05:06

C#連接數(shù)據(jù)庫(kù)代碼

2009-07-31 17:01:21

C#存取Access數(shù)

2009-09-04 17:23:21

C#數(shù)據(jù)庫(kù)連接對(duì)象

2009-08-07 16:19:00

C#下數(shù)據(jù)庫(kù)編程

2013-03-27 09:47:01

Android開發(fā)SQAndroid SDK
點(diǎn)贊
收藏

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