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

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

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

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

準(zhǔn)備工作

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

示例代碼

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

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

首先,我們需要創(chuàng)建一個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();

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

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

2. 創(chuàng)建表

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

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é)論

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

責(zé)任編輯:趙寧寧 來源: 后端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#動態(tài)調(diào)用WebSe

2011-03-17 15:59:37

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

2010-12-20 09:44:36

SQLite.C#

2009-08-11 13:35:13

C# Berkeley

2024-04-18 09:56:16

2009-08-05 16:49:42

C#中調(diào)用dll

2009-08-11 14:51:47

C#讀取Excel中數(shù)

2009-03-19 10:08:09

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

2011-08-30 14:15:34

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

2013-04-01 10:49:51

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

2009-08-07 16:19:00

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

2009-08-25 12:50:32

數(shù)據(jù)庫常用C#代碼

2009-08-12 14:27:36

訪問MySQL數(shù)據(jù)庫C# ODBC

2010-10-26 15:21:11

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

2009-09-04 17:23:21

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

2009-08-07 16:19:00

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

2009-08-06 18:10:06

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

2009-08-25 14:05:06

C#連接數(shù)據(jù)庫代碼
點贊
收藏

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