.NET 帮助 DuckDB C#(开发者用法) Jacob Mellor 已更新:2026年1月18日 下载 IronPDF NuGet 下载 DLL 下载 Windows 安装程序 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 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)}"); } } } $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(); $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 } $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(); $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"); } } $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"); } } } $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"; $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 生成功能。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。Jacob 拥有曼彻斯特大学土木工程一级荣誉工程学士学位(BEng)(1998-2001 年)。他的旗舰产品 IronPDF 和 Iron Suite for .NET 库在全球的 NuGet 安装量已超过 3000 万次,其基础代码继续为全球使用的开发人员工具提供动力。Jacob 拥有 25 年的商业经验和 41 年的编码专业知识,他一直专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 相关文章 已更新2026年2月20日 架起 CLI 简洁性与 .NET 的桥梁:使用 IronPDF for .NET 的 Curl DotNet Jacob Mellor 通过 CurlDotNet 填补了这一空白,CurlDotNet 库的创建是为了将 cURL 的熟悉感带入 .NET 生态系统。 阅读更多 已更新2025年12月20日 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多 已更新2025年12月20日 C# String Equals(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 LazyCache C#(开发者用法)WebGrease .NET Core(对开发人...
已更新2026年2月20日 架起 CLI 简洁性与 .NET 的桥梁:使用 IronPDF for .NET 的 Curl DotNet Jacob Mellor 通过 CurlDotNet 填补了这一空白,CurlDotNet 库的创建是为了将 cURL 的熟悉感带入 .NET 生态系统。 阅读更多
已更新2025年12月20日 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多