C#實(shí)現(xiàn)文件讀寫到SQLite數(shù)據(jù)庫
《文件讀寫到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# textBox如何實(shí)時(shí)更新到最新行
這篇文章主要介紹了C# textBox如何實(shí)時(shí)更新到最新行問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
剖析設(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),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C#中IList<T>與List<T>的區(qū)別深入解析
本篇文章主要是對(duì)C#中IList<T>與List<T>的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01

