.NET幫助 LiteDB .NET(對開發者如何理解的工作) Curtis Chau 更新日期:7月 28, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article LiteDB 是一個簡單、快速且輕量的內嵌 .NET 文件資料庫。 LiteDB .NET 的靈感來自於 MongoDB 數據庫,其 API 與 MongoDB 官方 .NET API 非常相似。 LiteDB 是一個無伺服器的數據庫,非常適合小型專案和移動應用程式。 本文將為您提供在專案中利用 LiteDB 功能的準確指導。 我們還介紹了使用 IronPDF,這是一個由 Iron Software 製作的 .NET 庫,用於生成和操作 PDF,以及如何用它將 LiteDB 數據庫內容輸出為 PDF 以便查看和分享。 LiteDB 的主要功能 嵌入式數據庫: 無需單獨的伺服器。 LiteDB 在您的應用程式流程中運行。 單一數據文件: 您可以在單一數據庫文件中存儲所有數據,簡化部署和備份。 BSON 格式: 使用 BSON 格式進行存儲,確保快速的讀寫操作。 LINQ 支持: 完全支持 LINQ 查詢,使其對 .NET 開發人員更直觀。 ACID 交易: 通過支持 ACID 交易來確保數據完整性。 跨平台: 在 Windows、Linux 和 macOS 上運行。 在 .NET 項目中安裝 LiteDB 在 Visual Studio 中打開您的專案。 然後在解決方案資源管理器中右鍵單擊您的專案,選擇"管理 NuGet 套件"。 搜尋 LiteDB 並安裝以輕鬆將此數據庫解決方案合併到您的項目中。 或者,您可以使用包管理控制台進行安裝。 要在 NuGet 包管理控制台中安裝 LiteDB,請使用以下命令: Install-Package LiteDB 開始使用 LiteDB 安裝完成後,您可以在應用程式中開始使用 LiteDB。 讓我們通過一些例子來說明它的用法。 範例 1:創建和插入數據 首先,讓我們創建一個簡單的 Product 類來表示我們的數據: public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 接下來,我們將創建一個數據庫並插入一些產品: using LiteDB; using System; class Program { static void Main() { // Open the database (or create it if it doesn't exist) using (var db = new LiteDatabase(@"MyData.db")) { // Get a collection (or create, if it doesn't exist) var products = db.GetCollection<Product>("products"); // Create a list of products to insert into the database var productList = new[] { new Product { Id = 201, Name = "Apple", Price = 0.99m }, new Product { Id = 202, Name = "Banana", Price = 0.59m }, new Product { Id = 203, Name = "Orange", Price = 0.79m }, new Product { Id = 204, Name = "Grape", Price = 2.99m }, new Product { Id = 205, Name = "Watermelon", Price = 4.99m } }; // Insert each product into the collection foreach (var product in productList) { products.Insert(product); } Console.WriteLine("Product inserted successfully."); } } } using LiteDB; using System; class Program { static void Main() { // Open the database (or create it if it doesn't exist) using (var db = new LiteDatabase(@"MyData.db")) { // Get a collection (or create, if it doesn't exist) var products = db.GetCollection<Product>("products"); // Create a list of products to insert into the database var productList = new[] { new Product { Id = 201, Name = "Apple", Price = 0.99m }, new Product { Id = 202, Name = "Banana", Price = 0.59m }, new Product { Id = 203, Name = "Orange", Price = 0.79m }, new Product { Id = 204, Name = "Grape", Price = 2.99m }, new Product { Id = 205, Name = "Watermelon", Price = 4.99m } }; // Insert each product into the collection foreach (var product in productList) { products.Insert(product); } Console.WriteLine("Product inserted successfully."); } } } Imports LiteDB Imports System Friend Class Program Shared Sub Main() ' Open the database (or create it if it doesn't exist) Using db = New LiteDatabase("MyData.db") ' Get a collection (or create, if it doesn't exist) Dim products = db.GetCollection(Of Product)("products") ' Create a list of products to insert into the database Dim productList = { New Product With { .Id = 201, .Name = "Apple", .Price = 0.99D }, New Product With { .Id = 202, .Name = "Banana", .Price = 0.59D }, New Product With { .Id = 203, .Name = "Orange", .Price = 0.79D }, New Product With { .Id = 204, .Name = "Grape", .Price = 2.99D }, New Product With { .Id = 205, .Name = "Watermelon", .Price = 4.99D } } ' Insert each product into the collection For Each product In productList products.Insert(product) Next product Console.WriteLine("Product inserted successfully.") End Using End Sub End Class $vbLabelText $csharpLabel 代碼描述 此代碼初始化與名為 "MyData.db" 的 LiteDB 數據庫的連接並檢索一個名為 "products" 的集合。 然後它創建一個 Product 對象的數組,其中包含各種屬性,如 ID、名稱和價格。 數組中的每個產品都插入到數據庫中的 "products" 集合中。 在成功插入所有產品後,它會向控制台打印一條確認信息。 輸出如下: 範例: 精簡用戶數據管理 想像您正在開發一個管理用戶帳戶的移動應用程式。 每個用戶都有一個包含他們姓名、電子郵件地址、偏好(作為 JSON 對象存儲)和收藏商品列表的個人資料。 這裡是 LiteDB .NET 如何簡化您的數據存儲: 這段代碼定義了一個 User 類來表示用戶數據,和一個 UserManager 類來在 LiteDB .NET 數據庫中管理用戶操作。 using LiteDB; using System.Collections.Generic; public class User { [BsonId] public string Id { get; set; } public string Name { get; set; } public string Email { get; set; } public Dictionary<string, string> Preferences { get; set; } public List<string> FavoriteItems { get; set; } } public class UserManager { private readonly LiteDatabase db; public UserManager(string connectionString) { db = new LiteDatabase(connectionString); } public void SaveUser(User user) { var collection = db.GetCollection<User>("users"); collection.Insert(user); } public User GetUser(string userId) { var collection = db.GetCollection<User>("users"); return collection.FindById(userId); } public void UpdateUser(User user) { var collection = db.GetCollection<User>("users"); collection.Update(user); } public void DeleteUser(string userId) { var collection = db.GetCollection<User>("users"); collection.Delete(userId); } } using LiteDB; using System.Collections.Generic; public class User { [BsonId] public string Id { get; set; } public string Name { get; set; } public string Email { get; set; } public Dictionary<string, string> Preferences { get; set; } public List<string> FavoriteItems { get; set; } } public class UserManager { private readonly LiteDatabase db; public UserManager(string connectionString) { db = new LiteDatabase(connectionString); } public void SaveUser(User user) { var collection = db.GetCollection<User>("users"); collection.Insert(user); } public User GetUser(string userId) { var collection = db.GetCollection<User>("users"); return collection.FindById(userId); } public void UpdateUser(User user) { var collection = db.GetCollection<User>("users"); collection.Update(user); } public void DeleteUser(string userId) { var collection = db.GetCollection<User>("users"); collection.Delete(userId); } } Imports LiteDB Imports System.Collections.Generic Public Class User <BsonId> Public Property Id() As String Public Property Name() As String Public Property Email() As String Public Property Preferences() As Dictionary(Of String, String) Public Property FavoriteItems() As List(Of String) End Class Public Class UserManager Private ReadOnly db As LiteDatabase Public Sub New(ByVal connectionString As String) db = New LiteDatabase(connectionString) End Sub Public Sub SaveUser(ByVal user As User) Dim collection = db.GetCollection(Of User)("users") collection.Insert(user) End Sub Public Function GetUser(ByVal userId As String) As User Dim collection = db.GetCollection(Of User)("users") Return collection.FindById(userId) End Function Public Sub UpdateUser(ByVal user As User) Dim collection = db.GetCollection(Of User)("users") collection.Update(user) End Sub Public Sub DeleteUser(ByVal userId As String) Dim collection = db.GetCollection(Of User)("users") collection.Delete(userId) End Sub End Class $vbLabelText $csharpLabel 此實現有效地利用了 LiteDb.NET 在用戶數據管理能力上。 User 類存儲用戶信息,而 UserManager 類提供方法在數據庫中保存、檢索、更新和刪除用戶。 LiteDB,適用於 .NET 的內嵌 NoSQL 數據庫 LiteDB 非常適合不需要用戶並發的中小型應用程式。 例如,它非常適合用於想要簡單快速存儲數據的個人控制台應用程序。 完全用 C# 開發,其輕量化不到 450KB,且不依賴外部依賴項。 他們 GitHub 頁面上列出來的一些要點: 無伺服器 NoSQL 文件存儲 簡單的 API,類似 MongoDB 線程安全 完全用 C# 編寫,LiteDB 與 .NET 4.5、NETStandard 1.3/2.0 兼容,打包成單個 DLL 文件,佔用不到 450KB。 提供完整交易支持的 ACID 寫入失敗後的數據恢復(WAL 日誌文件) 使用 AES 加密進行數據文件加密 您可以輕鬆地將您的逗比 CLRO 對象(POCO)類映射到 BsonDocument,使用屬性或 LiteDB 提供的 fluent mapper API。 存儲文件和流數據(如 MongoDB 中的 GridFS) 單一數據文件儲存(如 SQLite) 為快速搜索索引文檔字段 LINQ 查詢支持 SQL-Like 命令以存取/轉換數據 LiteDB Studio – 資料存取的精美 UI 開源並且免費供所有人使用 – 包括商業用途 IronPDF 介紹:一個 C# PDF 庫 IronPDF, a premier C# PDF library, facilitates seamless creation, editing, and 操作 .NET 工程中的 PDF 。 它為任務如 HTML 到 PDF 轉換,動態 PDF 生成和數據提取提供了全面的 API。 利用 .NET Chromium 引擎確保 HTML 到 PDF 文件的準確渲染,滿足多樣的項目需求,涵蓋 .NET Core,.NET Standard 和 .NET Framework。 IronPDF 保證從 HTML 內容生成 PDF 的準確性,簡單性和效率,支持網絡,桌面和控制台應用程式。 安裝 IronPDF 庫 要在您的項目中啟動 IronPDF,請通過 Visual Studio 中的 NuGet Package Manager 安裝該庫。 然後,只需遵循這些簡單的步驟: 打開 Visual Studio,並導航至解決方案瀏覽器。 右鍵單擊“依賴項”,選擇“管理 NuGet 套件”選項。 選擇“瀏覽”選項卡並搜索“ IronPdf ”。 選擇 IronPDF 並點擊“安裝”。 或者,在 Visual Studio 內,您可以通過執行以下命令來利用包管理控制台安裝該庫: Install-Package IronPdf 與 LiteDB 一同使用 IronPDF 的例子 這是一個簡單的代碼示例,說明使用 IronPDF 從 HTML 內容生成 PDF,採用 "using" 語句以確保資源正確釋放。 這裡展示了如何將 LiteDB 和 IronPDF 的功能結合起來,顯示如何將 LiteDB 中的數據輸出為 PDF 以供瀏覽: using LiteDB; using System; using System.Collections.Generic; using System.Linq; using System.Text; using IronPdf; class Program { static void Main() { using (var db = new LiteDatabase(@"MyData.db")) { // Retrieve the 'products' collection or create it var products = db.GetCollection<Product>("products"); // Add some initial products to the collection var productList = new[] { new Product { Id = 101, Name = "Apple", Price = 0.99m }, new Product { Id = 102, Name = "Banana", Price = 0.59m }, new Product { Id = 103, Name = "Orange", Price = 0.79m }, new Product { Id = 104, Name = "Grape", Price = 2.99m }, new Product { Id = 105, Name = "Watermelon", Price = 4.99m } }; // Insert products into the LiteDB collection foreach (var product in productList) { products.Insert(product); } Console.WriteLine("Product inserted successfully."); // Fetch all products from the database var allProducts = GetAllProducts(db); // Generate HTML content from the product list string htmlContent = GenerateHtml(allProducts); // Generate the PDF from the HTML content GeneratePDF(htmlContent); Console.WriteLine("PDF generated successfully."); } } public static List<Product> GetAllProducts(LiteDatabase db) { var products = db.GetCollection<Product>("products"); return products.FindAll().ToList(); } public static void GeneratePDF(string data) { // Set your IronPDF license key here IronPdf.License.LicenseKey = "Your-License-Key"; Console.WriteLine("PDF Generating Started..."); // Create a PDF renderer var renderer = new ChromePdfRenderer(); Console.WriteLine("PDF Processing ...."); // Render the HTML as a PDF var pdf = renderer.RenderHtmlAsPdf(data); // Save the PDF to a file string filePath = "Data.pdf"; pdf.SaveAs(filePath); Console.WriteLine($"PDF Generation Completed, File Saved as {filePath}"); } public static string GenerateHtml(List<Product> products) { // Build HTML table from product list StringBuilder htmlBuilder = new StringBuilder(); htmlBuilder.Append("<html><head><style>table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid black; padding: 8px; text-align: left; }</style></head><body>"); htmlBuilder.Append("<h1>Product List</h1>"); htmlBuilder.Append("<table><tr><th>ID</th><th>Name</th><th>Price</th></tr>"); // Add each product row to the HTML table foreach (var product in products) { htmlBuilder.Append($"<tr><td>{product.Id}</td><td>{product.Name}</td><td>{product.Price:C}</td></tr>"); } htmlBuilder.Append("</table></body></html>"); return htmlBuilder.ToString(); } } using LiteDB; using System; using System.Collections.Generic; using System.Linq; using System.Text; using IronPdf; class Program { static void Main() { using (var db = new LiteDatabase(@"MyData.db")) { // Retrieve the 'products' collection or create it var products = db.GetCollection<Product>("products"); // Add some initial products to the collection var productList = new[] { new Product { Id = 101, Name = "Apple", Price = 0.99m }, new Product { Id = 102, Name = "Banana", Price = 0.59m }, new Product { Id = 103, Name = "Orange", Price = 0.79m }, new Product { Id = 104, Name = "Grape", Price = 2.99m }, new Product { Id = 105, Name = "Watermelon", Price = 4.99m } }; // Insert products into the LiteDB collection foreach (var product in productList) { products.Insert(product); } Console.WriteLine("Product inserted successfully."); // Fetch all products from the database var allProducts = GetAllProducts(db); // Generate HTML content from the product list string htmlContent = GenerateHtml(allProducts); // Generate the PDF from the HTML content GeneratePDF(htmlContent); Console.WriteLine("PDF generated successfully."); } } public static List<Product> GetAllProducts(LiteDatabase db) { var products = db.GetCollection<Product>("products"); return products.FindAll().ToList(); } public static void GeneratePDF(string data) { // Set your IronPDF license key here IronPdf.License.LicenseKey = "Your-License-Key"; Console.WriteLine("PDF Generating Started..."); // Create a PDF renderer var renderer = new ChromePdfRenderer(); Console.WriteLine("PDF Processing ...."); // Render the HTML as a PDF var pdf = renderer.RenderHtmlAsPdf(data); // Save the PDF to a file string filePath = "Data.pdf"; pdf.SaveAs(filePath); Console.WriteLine($"PDF Generation Completed, File Saved as {filePath}"); } public static string GenerateHtml(List<Product> products) { // Build HTML table from product list StringBuilder htmlBuilder = new StringBuilder(); htmlBuilder.Append("<html><head><style>table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid black; padding: 8px; text-align: left; }</style></head><body>"); htmlBuilder.Append("<h1>Product List</h1>"); htmlBuilder.Append("<table><tr><th>ID</th><th>Name</th><th>Price</th></tr>"); // Add each product row to the HTML table foreach (var product in products) { htmlBuilder.Append($"<tr><td>{product.Id}</td><td>{product.Name}</td><td>{product.Price:C}</td></tr>"); } htmlBuilder.Append("</table></body></html>"); return htmlBuilder.ToString(); } } Imports LiteDB Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports IronPdf Friend Class Program Shared Sub Main() Using db = New LiteDatabase("MyData.db") ' Retrieve the 'products' collection or create it Dim products = db.GetCollection(Of Product)("products") ' Add some initial products to the collection Dim productList = { New Product With { .Id = 101, .Name = "Apple", .Price = 0.99D }, New Product With { .Id = 102, .Name = "Banana", .Price = 0.59D }, New Product With { .Id = 103, .Name = "Orange", .Price = 0.79D }, New Product With { .Id = 104, .Name = "Grape", .Price = 2.99D }, New Product With { .Id = 105, .Name = "Watermelon", .Price = 4.99D } } ' Insert products into the LiteDB collection For Each product In productList products.Insert(product) Next product Console.WriteLine("Product inserted successfully.") ' Fetch all products from the database Dim allProducts = GetAllProducts(db) ' Generate HTML content from the product list Dim htmlContent As String = GenerateHtml(allProducts) ' Generate the PDF from the HTML content GeneratePDF(htmlContent) Console.WriteLine("PDF generated successfully.") End Using End Sub Public Shared Function GetAllProducts(ByVal db As LiteDatabase) As List(Of Product) Dim products = db.GetCollection(Of Product)("products") Return products.FindAll().ToList() End Function Public Shared Sub GeneratePDF(ByVal data As String) ' Set your IronPDF license key here IronPdf.License.LicenseKey = "Your-License-Key" Console.WriteLine("PDF Generating Started...") ' Create a PDF renderer Dim renderer = New ChromePdfRenderer() Console.WriteLine("PDF Processing ....") ' Render the HTML as a PDF Dim pdf = renderer.RenderHtmlAsPdf(data) ' Save the PDF to a file Dim filePath As String = "Data.pdf" pdf.SaveAs(filePath) Console.WriteLine($"PDF Generation Completed, File Saved as {filePath}") End Sub Public Shared Function GenerateHtml(ByVal products As List(Of Product)) As String ' Build HTML table from product list Dim htmlBuilder As New StringBuilder() htmlBuilder.Append("<html><head><style>table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid black; padding: 8px; text-align: left; }</style></head><body>") htmlBuilder.Append("<h1>Product List</h1>") htmlBuilder.Append("<table><tr><th>ID</th><th>Name</th><th>Price</th></tr>") ' Add each product row to the HTML table For Each product In products htmlBuilder.Append($"<tr><td>{product.Id}</td><td>{product.Name}</td><td>{product.Price:C}</td></tr>") Next product htmlBuilder.Append("</table></body></html>") Return htmlBuilder.ToString() End Function End Class $vbLabelText $csharpLabel 代碼連接到 LiteDB 數據庫,添加一個產品列表,檢索所有產品,並生成產品列表的 HTML 表示。然後使用 IronPDF 庫將此 HTML 內容用於創建 PDF 文件。 過程包括添加產品,獲取產品,將產品列表轉換為 HTML 和生成 PDF 的方法。 輸出 PDF 文件輸出 結論 LiteDB 為 C# 開發人員提供了一個輕量,無伺服器內嵌式文件數據庫解決方案,非常適合小型專案和移動應用程式,具有如受 MongoDB 啟發的 API,嵌入式數據庫和跨平台兼容性之類的功能。 同時,IronPDF 作為一個優秀的 C# PDF 庫出現,簡化 .NET 項目中的 PDF 生成和操作,通過其 HTML 到 PDF 轉換和 NuGet 集成。 LiteDB 和 IronPDF 都為開發人員提供了有價值的工具,LiteDB 在數據庫管理上出色,而 IronPDF 在 PDF 處理方面卓越。 IronPDF 提供免費試用以解鎖其在 PDF 生成和操控上的全部潛能。 常見問題解答 如何在 C# 中將 HTML 內容轉換為 PDF? 您可以使用 IronPDF 在 C# 中將 HTML 內容轉換為 PDF。該庫提供了像 RenderHtmlAsPdf 這樣的方法,允許將 HTML 字串轉換為 PDF 文件。 將 LiteDB 與 .NET 專案集成的最佳方法是什麼? 要將 LiteDB 與 .NET 專案集成,可以在 Visual Studio 中使用 NuGet 包管理器來安裝 LiteDB。這樣可以讓您使用 C# 直接在應用中管理您的資料庫。 我可以如何從 LiteDB 資料生成 PDF? 要從 LiteDB 資料生成 PDF 您可以使用 IronPDF。通過從 LiteDB 提取數據並利用 IronPDF 的功能呈現它,您可以為報告或共享目的創建 PDF 文件。 我可以使用 IronPDF 在 C# 中操作現有 PDF 文件嗎? 是的,可以使用 IronPDF 操作現有的 PDF 文件。它提供在 C# 應用中編輯、合併和提取 PDF 內容的功能。 可以在移動應用程序中使用 LiteDB 嗎? 是的,LiteDB 非常適合移動應用程序,因為它輕量級,無伺服器,並能夠在單個文件中儲存數據。 LiteDB 集成的一些常見故障排除步驟是什麼? LiteDB 集成的一些常見故障排除步驟包括檢查通過 NuGet 正確安裝,確保您的數據庫文件路徑可訪問,並確認項目的 .NET 版本與 LiteDB 兼容。 如何確保使用 LiteDB 的數據完整性? LiteDB 支持 ACID 交易以保證數據的完整性和可靠性。您可以使用交易來保持一致性並處理並發數據修改。 在 .NET 中使用 IronPDF 生成 PDF 的好處是什麼? IronPDF 提供了易於 HTML 到 PDF 轉換的好處,高渲染準確性和全面的 PDF 操作功能,使其在 .NET 應用中生成和處理 PDF 的理想選擇。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 9月 4, 2025 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新日期 9月 4, 2025 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 更新日期 8月 5, 2025 C#開關模式匹配(對開發者來說是如何工作的) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 RestEase C#(對開發者如何理解的工作)FireSharp C#(對開發者如何...