C#實現(xiàn)文件讀寫到SQLite數(shù)據(jù)庫
更新時間:2025年01月08日 11:07:47 作者:碼農(nóng)君莫笑
這篇文章主要為大家詳細介紹了使用?C#?將文件讀寫到?SQLite?數(shù)據(jù)庫的幾種方法,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下
《文件讀寫到SQLite數(shù)據(jù)庫的方法》博客中,介紹了文件讀寫到SQLite數(shù)據(jù)庫的方法,例子使用的Python語言,本文是使用 C# 將文件讀寫到 SQLite 數(shù)據(jù)庫的幾種方法的具體示例,如果有些概念模糊可參考作者以前博客。
1. 使用 BLOB 存儲文件
示例代碼:
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); // 假設讀取 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. 存儲文件路徑
示例代碼:
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); // 假設讀取 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. 分塊存儲文件
示例代碼:
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); // 假設文件 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#實現(xiàn)文件讀寫到SQLite數(shù)據(jù)庫的詳細內(nèi)容,更多關于C#文件讀寫到數(shù)據(jù)庫的資料請關注腳本之家其它相關文章!
相關文章
c#打印預覽控件中實現(xiàn)用鼠標移動頁面功能代碼分享
項目中需要實現(xiàn)以下功能:打印預覽控件中,可以用鼠標拖動頁面,以查看超出顯示范圍之外的部分內(nèi)容,下面就是實現(xiàn)代碼2013-12-12wpf將表中數(shù)據(jù)顯示到datagrid示例
這篇文章主要介紹了wpf將表中數(shù)據(jù)顯示到datagrid示例,需要的朋友可以參考下2014-02-02C#在運行時動態(tài)創(chuàng)建類型的實現(xiàn)方法
這篇文章主要介紹了C#在運行時動態(tài)創(chuàng)建類型的實現(xiàn)方法,主要通過動態(tài)生成C#代碼再編譯成程序集來實現(xiàn)動態(tài)創(chuàng)建類型的,需要的朋友可以參考下2014-09-09C#版的 Escape() 和 Unescape() 函數(shù)分享
從網(wǎng)上看到兩個方法, C# 版的 Escape() 和 Unescape(),收藏下。2011-05-05