最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)文件讀寫到SQLite數(shù)據(jù)庫

 更新時(shí)間:2025年01月08日 11:07:47   作者:碼農(nóng)君莫笑  
這篇文章主要為大家詳細(xì)介紹了使用?C#?將文件讀寫到?SQLite?數(shù)據(jù)庫的幾種方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下

《文件讀寫到SQLite數(shù)據(jù)庫的方法》博客中,介紹了文件讀寫到SQLite數(shù)據(jù)庫的方法,例子使用的Python語言,本文是使用 C# 將文件讀寫到 SQLite 數(shù)據(jù)庫的幾種方法的具體示例,如果有些概念模糊可參考作者以前博客。

1. 使用 BLOB 存儲(chǔ)文件

示例代碼:

using System;
using System.Data.SQLite;
using System.IO;
 
class Program
{
    static void Main(string[] args)
    {
        string databasePath = "example.db";
        string connectionString = $"Data Source={databasePath};Version=3;";
 
        // 創(chuàng)建數(shù)據(jù)庫和表
        using (var connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            string createTableQuery = @"
                CREATE TABLE IF NOT EXISTS Files (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    name TEXT NOT NULL,
                    data BLOB NOT NULL
                );";
            using (var command = new SQLiteCommand(createTableQuery, connection))
            {
                command.ExecuteNonQuery();
            }
        }
 
        // 插入文件到數(shù)據(jù)庫
        string filePath = "example.pdf"; // 替換為你的文件路徑
        InsertFile(filePath, connectionString);
 
        // 從數(shù)據(jù)庫讀取文件
        ReadFile(1, connectionString); // 假設(shè)讀取 ID 為 1 的文件
    }
 
    static void InsertFile(string filePath, string connectionString)
    {
        byte[] fileData = File.ReadAllBytes(filePath);
        string fileName = Path.GetFileName(filePath);
 
        using (var connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            string insertQuery = "INSERT INTO Files (name, data) VALUES (@name, @data)";
            using (var command = new SQLiteCommand(insertQuery, connection))
            {
                command.Parameters.AddWithValue("@name", fileName);
                command.Parameters.AddWithValue("@data", fileData);
                command.ExecuteNonQuery();
            }
        }
 
        Console.WriteLine($"File '{fileName}' has been saved to the database.");
    }
 
    static void ReadFile(int fileId, string connectionString)
    {
        using (var connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            string selectQuery = "SELECT name, data FROM Files WHERE id = @id";
            using (var command = new SQLiteCommand(selectQuery, connection))
            {
                command.Parameters.AddWithValue("@id", fileId);
                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        string fileName = reader.GetString(0);
                        byte[] fileData = (byte[])reader["data"];
 
                        File.WriteAllBytes(fileName, fileData);
                        Console.WriteLine($"File '{fileName}' has been restored from the database.");
                    }
                }
            }
        }
    }
}

2. 存儲(chǔ)文件路徑

示例代碼:

using System;
using System.Data.SQLite;
 
class Program
{
    static void Main(string[] args)
    {
        string databasePath = "example.db";
        string connectionString = $"Data Source={databasePath};Version=3;";
 
        // 創(chuàng)建數(shù)據(jù)庫和表
        using (var connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            string createTableQuery = @"
                CREATE TABLE IF NOT EXISTS FilePaths (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    name TEXT NOT NULL,
                    path TEXT NOT NULL
                );";
            using (var command = new SQLiteCommand(createTableQuery, connection))
            {
                command.ExecuteNonQuery();
            }
        }
 
        // 插入文件路徑
        string filePath = "example.pdf"; // 替換為你的文件路徑
        InsertFilePath(filePath, connectionString);
 
        // 從數(shù)據(jù)庫讀取文件路徑
        GetFilePath(1, connectionString); // 假設(shè)讀取 ID 為 1 的文件路徑
    }
 
    static void InsertFilePath(string filePath, string connectionString)
    {
        string fileName = System.IO.Path.GetFileName(filePath);
 
        using (var connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            string insertQuery = "INSERT INTO FilePaths (name, path) VALUES (@name, @path)";
            using (var command = new SQLiteCommand(insertQuery, connection))
            {
                command.Parameters.AddWithValue("@name", fileName);
                command.Parameters.AddWithValue("@path", filePath);
                command.ExecuteNonQuery();
            }
        }
 
        Console.WriteLine($"File path '{filePath}' has been saved to the database.");
    }
 
    static void GetFilePath(int fileId, string connectionString)
    {
        using (var connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            string selectQuery = "SELECT name, path FROM FilePaths WHERE id = @id";
            using (var command = new SQLiteCommand(selectQuery, connection))
            {
                command.Parameters.AddWithValue("@id", fileId);
                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        string fileName = reader.GetString(0);
                        string filePath = reader.GetString(1);
 
                        Console.WriteLine($"File '{fileName}' is located at '{filePath}'.");
                    }
                }
            }
        }
    }
}

3. 分塊存儲(chǔ)文件

示例代碼:

using System;
using System.Data.SQLite;
using System.IO;
 
class Program
{
    static void Main(string[] args)
    {
        string databasePath = "example.db";
        string connectionString = $"Data Source={databasePath};Version=3;";
 
        // 創(chuàng)建數(shù)據(jù)庫和表
        using (var connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            string createTableQuery = @"
                CREATE TABLE IF NOT EXISTS FileChunks (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    file_id INTEGER NOT NULL,
                    chunk_index INTEGER NOT NULL,
                    chunk_data BLOB NOT NULL
                );";
            using (var command = new SQLiteCommand(createTableQuery, connection))
            {
                command.ExecuteNonQuery();
            }
        }
 
        // 插入文件分塊
        string filePath = "example.pdf"; // 替換為你的文件路徑
        InsertFileChunks(filePath, 1, connectionString); // 假設(shè)文件 ID 為 1
 
        // 重新組裝文件
        ReassembleFile(1, "output.pdf", connectionString); // 輸出為 output.pdf
    }
 
    static void InsertFileChunks(string filePath, int fileId, string connectionString)
    {
        int chunkSize = 1024 * 1024; // 每塊大小為 1MB
        byte[] buffer = new byte[chunkSize];
        int chunkIndex = 0;
 
        using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        using (var connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            string insertQuery = "INSERT INTO FileChunks (file_id, chunk_index, chunk_data) VALUES (@file_id, @chunk_index, @chunk_data)";
            using (var command = new SQLiteCommand(insertQuery, connection))
            {
                int bytesRead;
                while ((bytesRead = fileStream.Read(buffer, 0, chunkSize)) > 0)
                {
                    byte[] chunkData = new byte[bytesRead];
                    Array.Copy(buffer, chunkData, bytesRead);
 
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("@file_id", fileId);
                    command.Parameters.AddWithValue("@chunk_index", chunkIndex++);
                    command.Parameters.AddWithValue("@chunk_data", chunkData);
                    command.ExecuteNonQuery();
                }
            }
        }
 
        Console.WriteLine($"File '{filePath}' has been stored in chunks.");
    }
 
    static void ReassembleFile(int fileId, string outputPath, string connectionString)
    {
        using (var connection = new SQLiteConnection(connectionString))
        {
            connection.Open();
            string selectQuery = "SELECT chunk_data FROM FileChunks WHERE file_id = @file_id ORDER BY chunk_index";
            using (var command = new SQLiteCommand(selectQuery, connection))
            {
                command.Parameters.AddWithValue("@file_id", fileId);
                using (var reader = command.ExecuteReader())
                using (var outputFile = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
                {
                    while (reader.Read())
                    {
                        byte[] chunkData = (byte[])reader["chunk_data"];
                        outputFile.Write(chunkData, 0, chunkData.Length);
                    }
                }
            }
        }
 
        Console.WriteLine($"File has been reassembled to '{outputPath}'.");
    }
}

以上就是C#實(shí)現(xiàn)文件讀寫到SQLite數(shù)據(jù)庫的詳細(xì)內(nèi)容,更多關(guān)于C#文件讀寫到數(shù)據(jù)庫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 如何在c#中使用opencv函數(shù)庫

    如何在c#中使用opencv函數(shù)庫

    這篇文章主要介紹了如何在c#中使用opencv,對(duì)圖像匹配處理感興趣的同學(xué)可以參考下
    2021-04-04
  • unity實(shí)現(xiàn)屏幕上寫字效果

    unity實(shí)現(xiàn)屏幕上寫字效果

    這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)屏幕上寫字效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • C#職責(zé)鏈模式實(shí)例詳解

    C#職責(zé)鏈模式實(shí)例詳解

    這篇文章主要介紹了C#職責(zé)鏈模式,以實(shí)例形式完整分析了C#職責(zé)鏈模式的相關(guān)技巧與實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • C# textBox如何實(shí)時(shí)更新到最新行

    C# textBox如何實(shí)時(shí)更新到最新行

    這篇文章主要介紹了C# textBox如何實(shí)時(shí)更新到最新行問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • C# log4net日志庫的用法小結(jié)

    C# log4net日志庫的用法小結(jié)

    log4net日志開源庫是用來控制日志文件大小,日志文件個(gè)數(shù),滾動(dòng)式覆蓋,自由控制日志打印等級(jí),今天通過本文給大家介紹C# log4net日志庫的用法小結(jié),感興趣的朋友一起看看吧
    2021-10-10
  • C#檢測是否有u盤插入的方法

    C#檢測是否有u盤插入的方法

    這篇文章主要介紹了C#檢測是否有u盤插入的方法,涉及C#操作硬件的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • 剖析設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用

    剖析設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用

    這篇文章主要介紹了設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用,理論上來說組合模式包含抽象構(gòu)件、樹葉構(gòu)件和樹枝構(gòu)件三個(gè)角色,需要的朋友可以參考下
    2016-02-02
  • c#中使用BackgroundWorker的實(shí)現(xiàn)

    c#中使用BackgroundWorker的實(shí)現(xiàn)

    本文主要介紹了c#中使用BackgroundWorker的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C# 中的??操作符淺談

    C# 中的??操作符淺談

    (??) 用于如果類不為空值時(shí)返回它自身,如果為空值則返回之后的操作
    2013-04-04
  • C#中IList<T>與List<T>的區(qū)別深入解析

    C#中IList<T>與List<T>的區(qū)別深入解析

    本篇文章主要是對(duì)C#中IList<T>與List<T>的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2014-01-01

最新評(píng)論

洞头县| 怀安县| 高碑店市| 牟定县| 富蕴县| 涡阳县| 壶关县| 左贡县| 余姚市| 南召县| 湖口县| 库伦旗| 黄骅市| 鹤峰县| 泰顺县| 汝城县| 天峻县| 会同县| 嵩明县| 米脂县| 宜兰市| 突泉县| 新营市| 威信县| 志丹县| 临湘市| 即墨市| 浦县| 平江县| 利川市| 桓仁| 林芝县| 正镶白旗| 玉龙| 远安县| 敖汉旗| 开封县| 泸水县| 新化县| 遂溪县| 辰溪县|