.NET 帮助 DuckDB C#(开发者用法) Curtis Chau 已更新:七月 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 是一个用于 DuckDB 本机库的 .NET 绑定的开源提供程序,旨在与 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 包管理器进行安装。 基本用法 安装后,您可以开始在 C# 应用程序中使用 DuckDB.NET 执行 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 与数据框的集成 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 是一个方便的工具,可让您将网页、URL 和 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 文档。 从 URL、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 包。 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 代码解释 代码旨在展示如何使用 DuckDB.NET 进行数据库操作,并使用 IronPDF 生成包含数据库查询结果的 PDF 报告。 关键组件 DuckDB.NET: DuckDBConnection: 建立与内存中 DuckDB 数据库文件(“Data Source=:memory:”)的连接。 这个连接在代码中用于执行 SQL 命令。 数据库操作: 表创建: 定义一个 SQL 命令(CREATE TABLE integers(book STRING, cost INTEGER);)以创建一个名为 integers 的表,表中包含 book(STRING)和 cost(INTEGER)列。 数据插入: 在 integers 表中插入行(INSERT INTO integers VALUES ('book1', 25), ('book2', 30), ('book3', 10);)。 数据检索: 执行 SELECT 查询(SELECT book, cost FROM integers;)以从 integers 表中获取数据。 检索到的数据格式化为 HTML(内容)并打印到控制台。 使用 IronPDF 生成 PDF: 将 HTML 渲染为 PDF: 使用 IronPDF 中的 ChromePdfRenderer 将 HTML 内容(content)转换为 PDF 文档(pdf)。 保存 PDF: 将生成的 PDF 保存为当前目录中的“AwesomeDuckDbNet.pdf”。 输出 PDF IronPDF 许可 IronPDF 包需要许可证才能运行。 在应用程序开始时在包被访问之前添加以下代码。 IronPdf.License.LicenseKey = "IRONPDF-KEY"; IronPdf.License.LicenseKey = "IRONPDF-KEY"; IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel IronPDF 的试用许可证可在 IronPDF 的试用许可证页面 获得。 结论 DuckDB.NET C# 包是一个强大的工具,可将 DuckDB 的分析能力集成到 .NET 应用程序中。 其易用性、对多种数据格式的支持以及与 C# 的无缝集成,使其成为开发数据密集型应用程序的开发人员的绝佳选择。 无论您是构建数据分析工具、ETL 管道还是其他数据驱动的应用程序,DuckDB.NET 都能帮助您高效地实现目标。 常见问题解答 DuckDB.NET 在 C# 应用程序中用于什么? DuckDB.NET 用于在 C# 应用程序中集成 DuckDB 本地库,通过 ADO.NET 提供程序为开发人员提供强大的分析能力。 如何在 C# 项目中安装 DuckDB.NET? 您可以使用 .NET CLI 命令 dotnet add package DuckDB.NET.Data.Full 或通过 Visual Studio 中的 NuGet 包管理器安装 DuckDB.NET。 如何使用 DuckDB.NET 执行 SQL 查询? 您可以通过与 DuckDBConnection 建立连接来使用 DuckDB.NET 执行 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 文件? 您可以使用 DuckDB.NET 的 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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新九月 4, 2025 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多 已更新九月 4, 2025 C# String Equals(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 已更新八月 5, 2025 C# Switch 模式匹配(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 LazyCache C#(开发者用法)WebGrease .NET Core(开发者用法)
已更新九月 4, 2025 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多