.NET幫助 DuckDB C#(對開發者如何理解的工作) 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 DuckDB.NET 是一個開源的 .NET 綁定的提供者,專為 DuckDB 原生庫設計,與 C# 無縫整合。 它提供了 ADO.NET 提供者,使得在 .NET 應用程序中輕鬆使用 DuckDB,一個低層次的綁定庫。 這個套件對於希望在 C# 環境中利用 DuckDB 強大分析能力的開發人員來說是理想選擇。 安裝 安裝 DuckDB.NET 非常簡單。 您可以使用 .NET CLI 將其添加到您的項目中: dotnet add package DuckDB.NET.Data.Full dotnet add package DuckDB.NET.Data.Full SHELL 或者,您可以通過 Visual Studio 中的 NuGet Package Manager 來安裝它。 基本使用 安裝後,您可以開始使用 DuckDB.NET 在您的 C# 應用程序中執行 SQL 查詢。 這裡有一個簡單的例子: using System; using DuckDB.NET.Data; class Program { static void Main() { // Create and open a connection to an in-memory DuckDB database using var duckdbconnection = new DuckDBConnection("Data Source=:memory:"); duckdbconnection.Open(); // Create a command associated with the connection using var command = duckdbconnection.CreateCommand(); // Create a table named 'integers' command.CommandText = "CREATE TABLE integers(foo INTEGER, bar INTEGER);"; command.ExecuteNonQuery(); // Insert some data into the 'integers' table command.CommandText = "INSERT INTO integers VALUES (3, 4), (5, 6), (7, 8);"; command.ExecuteNonQuery(); // Retrieve the count of rows in the 'integers' table command.CommandText = "SELECT count(*) FROM integers"; var executeScalar = command.ExecuteScalar(); // Select all values from the 'integers' table command.CommandText = "SELECT foo, bar FROM integers;"; // Execute the query and process the results using var reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine($"{reader.GetInt32(0)}, {reader.GetInt32(1)}"); } } } using System; using DuckDB.NET.Data; class Program { static void Main() { // Create and open a connection to an in-memory DuckDB database using var duckdbconnection = new DuckDBConnection("Data Source=:memory:"); duckdbconnection.Open(); // Create a command associated with the connection using var command = duckdbconnection.CreateCommand(); // Create a table named 'integers' command.CommandText = "CREATE TABLE integers(foo INTEGER, bar INTEGER);"; command.ExecuteNonQuery(); // Insert some data into the 'integers' table command.CommandText = "INSERT INTO integers VALUES (3, 4), (5, 6), (7, 8);"; command.ExecuteNonQuery(); // Retrieve the count of rows in the 'integers' table command.CommandText = "SELECT count(*) FROM integers"; var executeScalar = command.ExecuteScalar(); // Select all values from the 'integers' table command.CommandText = "SELECT foo, bar FROM integers;"; // Execute the query and process the results using var reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine($"{reader.GetInt32(0)}, {reader.GetInt32(1)}"); } } } Imports System Imports DuckDB.NET.Data Friend Class Program Shared Sub Main() ' Create and open a connection to an in-memory DuckDB database Dim duckdbconnection As New DuckDBConnection("Data Source=:memory:") duckdbconnection.Open() ' Create a command associated with the connection Dim command = duckdbconnection.CreateCommand() ' Create a table named 'integers' command.CommandText = "CREATE TABLE integers(foo INTEGER, bar INTEGER);" command.ExecuteNonQuery() ' Insert some data into the 'integers' table command.CommandText = "INSERT INTO integers VALUES (3, 4), (5, 6), (7, 8);" command.ExecuteNonQuery() ' Retrieve the count of rows in the 'integers' table command.CommandText = "SELECT count(*) FROM integers" Dim executeScalar = command.ExecuteScalar() ' Select all values from the 'integers' table command.CommandText = "SELECT foo, bar FROM integers;" ' Execute the query and process the results Dim reader = command.ExecuteReader() Do While reader.Read() Console.WriteLine($"{reader.GetInt32(0)}, {reader.GetInt32(1)}") Loop End Sub End Class $vbLabelText $csharpLabel 這個例子演示了如何使用 DuckDB.NET 創建表格、插入數據和查詢數據。 輸出 數據引入 DuckDB.NET 支持從各種格式讀取數據,包括 CSV 和 Parquet 文件。 以下是如何從 CSV 文件中讀取數據: command.CommandText = "COPY integers FROM 'example.csv' (FORMAT CSV);"; command.ExecuteNonQuery(); command.CommandText = "COPY integers FROM 'example.csv' (FORMAT CSV);"; command.ExecuteNonQuery(); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 與 DataFrames 的集成 DuckDB.NET 還可以與數據框集成,允許您使用熟悉的 SQL 語法操作數據。 這對於數據分析任務特別有用。 結果轉換 您可以將查詢結果轉換為各種格式,例如列表或自定義對象,使您在應用程序中方便地使用數據: var results = new List<(int foo, int bar)>(); // Read and store results to a List while (reader.Read()) { results.Add((reader.GetInt32(0), reader.GetInt32(1))); // You can also use a loop with an index to iterate the results } var results = new List<(int foo, int bar)>(); // Read and store results to a List while (reader.Read()) { results.Add((reader.GetInt32(0), reader.GetInt32(1))); // You can also use a loop with an index to iterate the results } Dim results = New List(Of (foo As Integer, bar As Integer))() ' Read and store results to a List Do While reader.Read() results.Add((reader.GetInt32(0), reader.GetInt32(1))) ' You can also use a loop with an index to iterate the results Loop $vbLabelText $csharpLabel 將數據寫入磁碟 DuckDB.NET 支持以各種格式將數據寫入磁碟。 您可以使用 COPY 語句將數據導出到 CSV 文件: command.CommandText = "COPY integers TO 'output.csv' (FORMAT CSV);"; command.ExecuteNonQuery(); command.CommandText = "COPY integers TO 'output.csv' (FORMAT CSV);"; command.ExecuteNonQuery(); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel IronPDF 简介 IronPDF 是一個 C# PDF 庫,可在 .NET 項目中生成、管理和提取 PDF 文檔內容。 以下是一些關鍵功能: IronPDF 是一個方便的工具,可以將網頁、網址和HTML轉換為PDF。 最佳部分? PDF 看起來與原網頁完全相同——保留所有格式和樣式。 所以,如果您需要從在線內容(如報告或發票)製作 PDF,那麼 IronPDF 是您的首選。 using IronPdf; class Program { static void Main(string[] args) { var renderer = new ChromePdfRenderer(); // 1. Convert HTML String to PDF var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"; var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent); pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf"); // 2. Convert HTML File to PDF var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath); pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf"); // 3. Convert URL to PDF var url = "http://ironpdf.com"; // Specify the URL var pdfFromUrl = renderer.RenderUrlAsPdf(url); pdfFromUrl.SaveAs("URLToPDF.pdf"); } } using IronPdf; class Program { static void Main(string[] args) { var renderer = new ChromePdfRenderer(); // 1. Convert HTML String to PDF var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"; var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent); pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf"); // 2. Convert HTML File to PDF var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath); pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf"); // 3. Convert URL to PDF var url = "http://ironpdf.com"; // Specify the URL var pdfFromUrl = renderer.RenderUrlAsPdf(url); pdfFromUrl.SaveAs("URLToPDF.pdf"); } } Imports IronPdf Friend Class Program Shared Sub Main(ByVal args() As String) Dim renderer = New ChromePdfRenderer() ' 1. Convert HTML String to PDF Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>" Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent) pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf") ' 2. Convert HTML File to PDF Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath) pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf") ' 3. Convert URL to PDF Dim url = "http://ironpdf.com" ' Specify the URL Dim pdfFromUrl = renderer.RenderUrlAsPdf(url) pdfFromUrl.SaveAs("URLToPDF.pdf") End Sub End Class $vbLabelText $csharpLabel HTML 到 PDF 轉換: 將 HTML、CSS 和 JavaScript 內容轉換為 PDF。 使用 Chrome 渲染引擎來實現像素級完美的 PDF 文檔。 從網址、HTML 文件或 HTML 字串生成 PDF。 圖像和內容轉換: 將圖像轉換為 PDF 文檔或從 PDF 文檔中提取圖像。 支持多種圖像格式,如 JPG、PNG 等。 設置 PDF 文檔的屬性、安全性和權限。 編輯和操縱: 為 PDF 添加數位簽名。 編輯元數據和修訂歷史。 兼容 .NET Core(8、7、6、5 和 3.1+)、.NET Standard(2.0+)和 .NET Framework(4.6.2+)。 跨平台支持: 兼容 Windows、Linux 和 macOS。 可在 NuGet 上獲得,便於安裝。 使用 IronPDF 和 DuckDB .NET 生成 PDF 文檔 首先使用 Visual Studio 創建一個控制台應用程序,如下所示。 提供項目名稱。 提供 .NET 版本。 安裝 IronPDF 套件。 安裝 DuckDB.NET 套件。 該代碼旨在展示如何使用 DuckDB.NET 進行數據庫操作以及使用 IronPDF 生成包含數據庫查詢結果的 PDF 報告。 using DuckDB.NET.Data; using IronPdf; namespace CodeSample { public static class DuckDbDemo { public static void Execute() { // Instantiate Renderer var renderer = new ChromePdfRenderer(); var content = "<h1>Demo DuckDb and IronPDF</h1>"; content += "<h2>Create DuckDBConnection</h2>"; content += "<p>new DuckDBConnection(\"Data Source=:memory:\");</p>"; content += "<p></p>"; // Create and open a connection to an in-memory DuckDB database using var connection = new DuckDBConnection("Data Source=:memory:"); connection.Open(); using var command = connection.CreateCommand(); // Create a table named 'integers' command.CommandText = "CREATE TABLE integers(book STRING, cost INTEGER);"; command.ExecuteNonQuery(); content += "<p>CREATE TABLE integers(book STRING, cost INTEGER);</p>"; // Insert some data into the 'integers' table command.CommandText = "INSERT INTO integers VALUES ('book1', 25), ('book2', 30), ('book3', 10);"; command.ExecuteNonQuery(); content += "<p>INSERT INTO integers VALUES ('book1', 25), ('book2', 30), ('book3', 10);</p>"; // Select all values from the 'integers' table command.CommandText = "SELECT book, cost FROM integers;"; using var reader = command.ExecuteReader(); content += "<p>SELECT book, cost FROM integers;</p>"; // Execute the query and process the results, appending them to the HTML content while (reader.Read()) { content += $"<p>{reader.GetString(0)}, {reader.GetInt32(1)}</p>"; Console.WriteLine($"{reader.GetString(0)}, {reader.GetInt32(1)}"); } // Save data to CSV content += "<p>Save data to CSV with COPY integers TO 'output.csv' (FORMAT CSV);</p>"; command.CommandText = "COPY integers TO 'output.csv' (FORMAT CSV);"; command.ExecuteNonQuery(); // Generate and save PDF var pdf = renderer.RenderHtmlAsPdf(content); pdf.SaveAs("AwesomeDuckDbNet.pdf"); } } } using DuckDB.NET.Data; using IronPdf; namespace CodeSample { public static class DuckDbDemo { public static void Execute() { // Instantiate Renderer var renderer = new ChromePdfRenderer(); var content = "<h1>Demo DuckDb and IronPDF</h1>"; content += "<h2>Create DuckDBConnection</h2>"; content += "<p>new DuckDBConnection(\"Data Source=:memory:\");</p>"; content += "<p></p>"; // Create and open a connection to an in-memory DuckDB database using var connection = new DuckDBConnection("Data Source=:memory:"); connection.Open(); using var command = connection.CreateCommand(); // Create a table named 'integers' command.CommandText = "CREATE TABLE integers(book STRING, cost INTEGER);"; command.ExecuteNonQuery(); content += "<p>CREATE TABLE integers(book STRING, cost INTEGER);</p>"; // Insert some data into the 'integers' table command.CommandText = "INSERT INTO integers VALUES ('book1', 25), ('book2', 30), ('book3', 10);"; command.ExecuteNonQuery(); content += "<p>INSERT INTO integers VALUES ('book1', 25), ('book2', 30), ('book3', 10);</p>"; // Select all values from the 'integers' table command.CommandText = "SELECT book, cost FROM integers;"; using var reader = command.ExecuteReader(); content += "<p>SELECT book, cost FROM integers;</p>"; // Execute the query and process the results, appending them to the HTML content while (reader.Read()) { content += $"<p>{reader.GetString(0)}, {reader.GetInt32(1)}</p>"; Console.WriteLine($"{reader.GetString(0)}, {reader.GetInt32(1)}"); } // Save data to CSV content += "<p>Save data to CSV with COPY integers TO 'output.csv' (FORMAT CSV);</p>"; command.CommandText = "COPY integers TO 'output.csv' (FORMAT CSV);"; command.ExecuteNonQuery(); // Generate and save PDF var pdf = renderer.RenderHtmlAsPdf(content); pdf.SaveAs("AwesomeDuckDbNet.pdf"); } } } Imports DuckDB.NET.Data Imports IronPdf Namespace CodeSample Public Module DuckDbDemo Public Sub Execute() ' Instantiate Renderer Dim renderer = New ChromePdfRenderer() Dim content = "<h1>Demo DuckDb and IronPDF</h1>" content &= "<h2>Create DuckDBConnection</h2>" content &= "<p>new DuckDBConnection(""Data Source=:memory:"");</p>" content &= "<p></p>" ' Create and open a connection to an in-memory DuckDB database Dim connection = New DuckDBConnection("Data Source=:memory:") connection.Open() Dim command = connection.CreateCommand() ' Create a table named 'integers' command.CommandText = "CREATE TABLE integers(book STRING, cost INTEGER);" command.ExecuteNonQuery() content &= "<p>CREATE TABLE integers(book STRING, cost INTEGER);</p>" ' Insert some data into the 'integers' table command.CommandText = "INSERT INTO integers VALUES ('book1', 25), ('book2', 30), ('book3', 10);" command.ExecuteNonQuery() content &= "<p>INSERT INTO integers VALUES ('book1', 25), ('book2', 30), ('book3', 10);</p>" ' Select all values from the 'integers' table command.CommandText = "SELECT book, cost FROM integers;" Dim reader = command.ExecuteReader() content &= "<p>SELECT book, cost FROM integers;</p>" ' Execute the query and process the results, appending them to the HTML content Do While reader.Read() content &= $"<p>{reader.GetString(0)}, {reader.GetInt32(1)}</p>" Console.WriteLine($"{reader.GetString(0)}, {reader.GetInt32(1)}") Loop ' Save data to CSV content &= "<p>Save data to CSV with COPY integers TO 'output.csv' (FORMAT CSV);</p>" command.CommandText = "COPY integers TO 'output.csv' (FORMAT CSV);" command.ExecuteNonQuery() ' Generate and save PDF Dim pdf = renderer.RenderHtmlAsPdf(content) pdf.SaveAs("AwesomeDuckDbNet.pdf") End Sub End Module End Namespace $vbLabelText $csharpLabel 代码解释 關鍵組成部分 DuckDBConnection: 建立與內存中 DuckDB 數據庫文件("Data Source=:memory:")的連接。 DuckDBConnection: 建立與內存中 DuckDB 數據庫文件("Data Source=:memory:")的連接。 此連接在整個代碼中用於執行 SQL 命令。 2. 數據庫操作: 表創建: 定義 SQL 命令(CREATE TABLE integers(book STRING, cost INTEGER);)以創建名為 integers 的表,具有書籍(STRING)和成本(INTEGER)列。 數據插入: 往 integers 表中插入數行數據(INSERT INTO integers VALUES ('book1', 25), ('book2', 30), ('book3', 10);)。 數據檢索: 執行 SELECT 查詢(SELECT book, cost FROM integers;)以從 integers 表中獲取數據。 檢索到的數據被格式化為 HTML(content)並打印到控制台。 3. 使用 IronPDF 生成 PDF: 將 HTML 渲染為 PDF: 使用 IronPDF 的 ChromePdfRenderer 將 HTML 內容(content)轉換為 PDF 文檔(pdf)。 保存 PDF: 將生成的 PDF 保存為 "AwesomeDuckDbNet.pdf" 在當前目錄中。 輸出 IronPDF 套件運行需要授權。 IronPDF 許可證 在應用程序開始時添加下面的代碼,以便在訪問套件之前設置授權。 試用授權可以在 IronPDF 的試用授權頁面 上獲得。 IronPdf.License.LicenseKey = "IRONPDF-KEY"; IronPdf.License.LicenseKey = "IRONPDF-KEY"; IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel DuckDB.NET C# 套件是一個強大的工具,能夠將 DuckDB 的分析能力集成到 .NET 應用程序中。 結論 其易用性、對各種數據格式的支持以及與 C# 的無縫集成使其成為從事數據密集型應用程序的開發人員的絕佳選擇。 無論您是在構建數據分析工具、ETL 管道,還是其他數據驅動應用程序,DuckDB.NET 都可以幫助您高效地實現目標。 Whether you are building data analysis tools, ETL pipelines, or other data-driven applications, DuckDB.NET can help you achieve your goals efficiently. 常見問題解答 DuckDB.NET 在 C# 應用程式中有什麼用途? DuckDB.NET 用於整合 DuckDB 原生庫至 C# 應用程式中,透過 ADO.NET 提供強大的分析能力給開發者。 如何在 C# 專案中安裝 DuckDB.NET? 您可以透過 .NET CLI 命令 dotnet add package DuckDB.NET.Data.Full 或在 Visual Studio 中透過 NuGet 套件管理員安裝 DuckDB.NET。 如何使用 DuckDB.NET 執行 SQL 查詢? 您可以建立與 DuckDBConnection 的連線,並執行 SQL 命令來建立表格、插入及檢索資料。 DuckDB.NET 能支援從 CSV 和 Parquet 檔案讀取資料嗎? 是的,DuckDB.NET 支援從包括 CSV 和 Parquet 檔案在內的多種格式進行資料攝取,允許在 C# 應用程式中無縫整合和操作這些資料類型。 怎樣在 C# 中將 HTML 轉換為 PDF? 您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字符串轉換為 PDF。您還可以使用 RenderHtmlFileAsPdf 將 HTML 文件轉換為 PDF。 使用 DuckDB.NET 在資料密集的專案中有哪些優點? DuckDB.NET 提供強大的分析能力,支援基於 SQL 的資料操作,並能與 C# 應用程式輕鬆整合,對於資料密集的專案極為理想。 DuckDB.NET 如何與資料框架整合? DuckDB.NET 可以與資料框架整合,支持基於 SQL 的資料操作,這對於執行複雜的資料分析任務特別有用。 如何使用 DuckDB.NET 將資料匯出為 CSV 檔案? 您可以使用 COPY 語句將資料匯出為 CSV 檔案。例如,使用 COPY integers TO 'output.csv' (FORMAT CSV); 將表格資料匯出為 CSV 檔案。 IronPDF 支持哪些平台? IronPDF 支援 .NET Core (8, 7, 6, 5 和 3.1+)、.NET Standard (2.0+)和 .NET Framework (4.6.2+),並且與 Windows、Linux 和 macOS 相容。 我可以結合 DuckDB.NET 和 IronPDF 來生成報告嗎? 是的,您可以將 DuckDB.NET 用於資料庫操作,將 IronPDF 用於生成 PDF 報告,利用 DuckDB 的資料庫功能結合 IronPDF 的 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時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 LazyCache C#(對開發者如何理解的工作)WebGrease .NET Core(對開發者...