C#實(shí)現(xiàn)文件讀寫(xiě)到SQLite數(shù)據(jù)庫(kù)
《文件讀寫(xiě)到SQLite數(shù)據(jù)庫(kù)的方法》博客中,介紹了文件讀寫(xiě)到SQLite數(shù)據(jù)庫(kù)的方法,例子使用的Python語(yǔ)言,本文是使用 C# 將文件讀寫(xiě)到 SQLite 數(shù)據(jù)庫(kù)的幾種方法的具體示例,如果有些概念模糊可參考作者以前博客。
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ù)庫(kù)和表
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ù)庫(kù)
string filePath = "example.pdf"; // 替換為你的文件路徑
InsertFile(filePath, connectionString);
// 從數(shù)據(jù)庫(kù)讀取文件
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ù)庫(kù)和表
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ù)庫(kù)讀取文件路徑
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ù)庫(kù)和表
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)文件讀寫(xiě)到SQLite數(shù)據(jù)庫(kù)的詳細(xì)內(nèi)容,更多關(guān)于C#文件讀寫(xiě)到數(shù)據(jù)庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于c#實(shí)現(xiàn)的九九乘法表(簡(jiǎn)單實(shí)例)
本文主要分享了基于c#實(shí)現(xiàn)的九九乘法表,代碼簡(jiǎn)潔,需要的朋友可以參考下,希望對(duì)大家有所幫助2016-12-12
C#實(shí)現(xiàn)簡(jiǎn)單的雙色球抽取中獎(jiǎng)號(hào)碼代碼
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的雙色球抽取中獎(jiǎng)號(hào)碼代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
c#打印預(yù)覽控件中實(shí)現(xiàn)用鼠標(biāo)移動(dòng)頁(yè)面功能代碼分享
項(xiàng)目中需要實(shí)現(xiàn)以下功能:打印預(yù)覽控件中,可以用鼠標(biāo)拖動(dòng)頁(yè)面,以查看超出顯示范圍之外的部分內(nèi)容,下面就是實(shí)現(xiàn)代碼2013-12-12
wpf將表中數(shù)據(jù)顯示到datagrid示例
這篇文章主要介紹了wpf將表中數(shù)據(jù)顯示到datagrid示例,需要的朋友可以參考下2014-02-02
C#在運(yùn)行時(shí)動(dòng)態(tài)創(chuàng)建類(lèi)型的實(shí)現(xiàn)方法
這篇文章主要介紹了C#在運(yùn)行時(shí)動(dòng)態(tài)創(chuàng)建類(lèi)型的實(shí)現(xiàn)方法,主要通過(guò)動(dòng)態(tài)生成C#代碼再編譯成程序集來(lái)實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建類(lèi)型的,需要的朋友可以參考下2014-09-09
C#基于OLEDB獲取Excel文件表結(jié)構(gòu)信息的方法
這篇文章主要介紹了C#基于OLEDB獲取Excel文件表結(jié)構(gòu)信息的方法,結(jié)合實(shí)例形式分析了OLEDB的調(diào)用及Excel的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
c#分頁(yè)顯示服務(wù)器上指定目錄下的所有圖片示例
這篇文章主要介紹了c#分頁(yè)顯示服務(wù)器上指定目錄下的所有圖片示例,需要的朋友可以參考下2014-05-05
C#版的 Escape() 和 Unescape() 函數(shù)分享
從網(wǎng)上看到兩個(gè)方法, C# 版的 Escape() 和 Unescape(),收藏下。2011-05-05

