跳至页脚内容
USING IRONBARCODE

How to Add a Barcode in Crystal Reports using C#

Crystal Reports是一款功能强大的报表工具,它允许开发人员为其应用程序创建功能丰富的报表。 在 Crystal Reports 中使用C#添加条形码时,它为数据表示增添了一个新的维度,使信息更容易管理和跟踪。 在本文中,我们将探讨使用 C# 将条形码集成到 Crystal Reports 中的步骤。

如何使用 C# 在 Crystal Reports 中添加条形码

  1. 安装条形码库。
  2. 生成条形码图像并将其作为图像保存到数据库表中。
  3. 设计 Crystal Report 的布局。
  4. 建立与数据库的连接并选择必要的表。
  5. 在 Crystal Report 布局中添加条形码图像字段。
  6. 构建并运行项目。

选择条形码库

在深入实施之前,选择一个支持 Crystal Reports 和 C# 的条形码库至关重要。 IronBarcode for .NET 是一个很受欢迎的选择。

什么是 IronBarcode

IronBarcode是一个功能强大的 .NET 库,可简化条形码的生成读取。 使用 IronBarcode,您可以通过指定要编码的值,轻松创建各种条形码,包括 Code 128 和 QR 码。 它还支持调整大小自定义。 在读取方面,IronBarcode 可以从图像或 PDF 中提取条形码数据,使其成为库存管理和文档跟踪的理想选择。 其用户友好的 API 确保您能快速集成到项目中,并且跨平台支持使您能够在不同的 .NET 版本间无缝开发。 无论您是经验丰富的开发人员还是初学者,IronBarcode 都能让您高效地处理条形码。

创建新项目

打开 Visual Studio 以创建 ASP.NET Crystal Reports 网站。我使用的是Visual Studio 2022 。 您可以使用任何版本,但请确保已安装适用于该特定版本的 Visual Studio 的 Crystal Reports。

![如何使用 C# 在 Crystal Reports 中添加条形码:图 1 - 打开 Visual Studio。 创建一个新的"ASP.NET Crystal Reports 网站"项目。

选择项目名称、地点和目标框架。 点击"创建"按钮。 将创建一个新项目,如下所示。

如何在 Crystal Reports 中使用 C# 添加条形码:图 2 - 将创建一个新项目,其中包含默认的 CrystalReport1.rpt 页面。

开始之前,我们需要一个数据库。 让我们创建一个新的数据库和一个示例表。

创建新数据库

以下脚本将创建一个新的数据库。

CREATE DATABASE ProductDB;
USE [ProductDB]
GO
/****** Object:  Table [dbo].[Products]    Script Date: 3/10/2024 2:57:18 PM******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Products](
    [Product_ID] [int] NULL,
    [Product_Name] [varchar](100) NULL,
    [Product_Price] [decimal](18, 0) NULL,
    [Product_Barcode] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

让我们向此表中插入数据。 我们将创建一个条形码并将其保存在产品表中。 因此,我们需要使用 C# 代码插入数据,并在我们的项目中安装 IronBarcode 库,以便使用其功能创建条形码。

安装 IronBarcode 库

要使用 NuGet 包管理器控制台安装IronBarcode 库,请按照以下步骤操作:

打开 NuGet 包管理器控制台。 您可以通过依次点击"查看"->"其他窗口"->"程序包管理器控制台"找到它。

如何使用 C# 在 Crystal Reports 中添加条形码:图 3 - 打开 NuGet 包管理器控制台。 导航至视图菜单 - 其他窗口 - 程序包管理器控制台

在软件包管理器控制台中,使用以下命令安装 IronBarcode 库:

Install-Package BarCode

按 Enter 执行命令。

或者,您可以使用"管理解决方案的 NuGet 程序包"来安装 IronBarcode 库:

如何在 Crystal Reports 中使用 C# 添加条形码:图 4 - 使用 NuGet 包管理器的搜索栏搜索"IronBarcode",然后选择项目并单击"安装"按钮,即可通过管理解决方案的 NuGet 包来安装 IronBarcode。

等待 NuGet 包管理器下载并安装 IronBarcode 库及其依赖项。 安装完成后,您将在软件包管理器控制台中看到确认消息。

现在,IronBarcode 库已安装到您的项目中,您可以开始使用它的条形码生成和读取功能。

生成条形码图像并将其存储在数据库中

我们将使用 ADO.NET 生成条形码图像并将其存储在数据库中。 以下代码演示了在 C# 中生成条形码的示例。

using System;
using System.Data.SqlClient;
using IronBarCode;

class Program
{
    static void Main(string[] args)
    {
        // Create a barcode from a string value using Code128 format.
        var myBarcode = BarcodeWriter.CreateBarcode("77446252", BarcodeWriterEncoding.Code128);
        // Add the barcode value text below the barcode image.
        myBarcode.AddBarcodeValueTextBelowBarcode();
        // Resize the barcode image.
        myBarcode.ResizeTo(600, 300);

        // SQL connection to the SQL Server database.
        using (SqlConnection cn = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=ProductDB;User ID=sa;Password=123456;Integrated Security=SSPI;"))
        {
            // SQL command to insert the barcode into the Products table.
            SqlCommand cmd = new SqlCommand($"INSERT INTO dbo.Products VALUES (77446252, 'Pine Apple Small', '100', @Barcode)", cn);
            // Add parameter for the barcode binary data.
            cmd.Parameters.AddWithValue("@Barcode", myBarcode.BinaryStream);

            // Open the connection, execute the query, close the connection.
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
    }
}
using System;
using System.Data.SqlClient;
using IronBarCode;

class Program
{
    static void Main(string[] args)
    {
        // Create a barcode from a string value using Code128 format.
        var myBarcode = BarcodeWriter.CreateBarcode("77446252", BarcodeWriterEncoding.Code128);
        // Add the barcode value text below the barcode image.
        myBarcode.AddBarcodeValueTextBelowBarcode();
        // Resize the barcode image.
        myBarcode.ResizeTo(600, 300);

        // SQL connection to the SQL Server database.
        using (SqlConnection cn = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=ProductDB;User ID=sa;Password=123456;Integrated Security=SSPI;"))
        {
            // SQL command to insert the barcode into the Products table.
            SqlCommand cmd = new SqlCommand($"INSERT INTO dbo.Products VALUES (77446252, 'Pine Apple Small', '100', @Barcode)", cn);
            // Add parameter for the barcode binary data.
            cmd.Parameters.AddWithValue("@Barcode", myBarcode.BinaryStream);

            // Open the connection, execute the query, close the connection.
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
    }
}
Imports System
Imports System.Data.SqlClient
Imports IronBarCode

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create a barcode from a string value using Code128 format.
		Dim myBarcode = BarcodeWriter.CreateBarcode("77446252", BarcodeWriterEncoding.Code128)
		' Add the barcode value text below the barcode image.
		myBarcode.AddBarcodeValueTextBelowBarcode()
		' Resize the barcode image.
		myBarcode.ResizeTo(600, 300)

		' SQL connection to the SQL Server database.
		Using cn As New SqlConnection("Data Source=localhost\SQLEXPRESS;Initial Catalog=ProductDB;User ID=sa;Password=123456;Integrated Security=SSPI;")
			' SQL command to insert the barcode into the Products table.
			Dim cmd As New SqlCommand($"INSERT INTO dbo.Products VALUES (77446252, 'Pine Apple Small', '100', @Barcode)", cn)
			' Add parameter for the barcode binary data.
			cmd.Parameters.AddWithValue("@Barcode", myBarcode.BinaryStream)

			' Open the connection, execute the query, close the connection.
			cn.Open()
			cmd.ExecuteNonQuery()
			cn.Close()
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

代码解释

1. 条形码生成

  1. var myBarcode = BarcodeWriter.CreateBarcode("77446252", BarcodeWriterEncoding.Code128);

    • 此行使用BarcodeWriter类的CreateBarcode()方法创建条形码。 条形码是根据字符串"77446252"表示的数据,采用 Code 128 编码格式生成的。 myBarcode变量现在保存着生成的条形码。

2.AddBarcodeValueTextBelowBarcode AddBarcodeValueTextBelowBarcode()

- 此功能会在条形码下方添加条形码值的描述文本。
  1. ResizeTo(600, 300)

    • 将条形码图像的高度和宽度调整为提供的尺寸。
  2. 您可以使用SaveAs()方法将条形码图像保存到文件系统中。

上述代码将生成以下条形码:

如何在 Crystal Reports 中使用 C# 添加条形码:图 5 - 输出:生成的条形码

2. 数据库连接设置

  1. SqlConnection cn = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=ProductDB;User ID=sa;Password=123456;Integrated Security=SSPI;");

    • 建立与 SQL Server 数据库的连接。 连接字符串指定服务器( localhost\\SQLEXPRESS )、初始目录(数据库名称: ProductDB )和身份验证凭据( user ID sapassword 123456 )。

3. SQL 命令创建

  1. SqlCommand cmd = new SqlCommand($"INSERT INTO dbo.Products VALUES (77446252, 'Pine Apple Small', '100', '{myBarcode.BinaryStream}' )", cn);

    • 创建一个新的SqlCommand对象。 此命令表示将数据插入 Products 表的 SQL 查询。
    • 该查询将值插入到表列中: 77446252'Pine Apple Small''100'和生成的条形码的二进制流 ( myBarcode.BinaryStream )。

4. 数据库交互

  1. cn.Open(); : 数据库连接已打开。
  2. cmd.ExecuteNonQuery(); : 执行 SQL 查询,将指定的值插入 Products 表中。
  3. cn.Close(); : 关闭数据库连接以释放资源。

设计您的 Crystal Reports 条形码生成器

现在,设计报表布局,添加数据库连接,并安排必要的字段。 如果你是新手,请按照以下步骤操作。

  1. 打开字段资源管理器 => 数据库字段 => 数据库专家。

如何在 Crystal Reports 中使用 C# 添加条形码:图 6 - 将数据库连接添加到报表布局:打开字段资源管理器 - 数据库字段 - 数据库专家。

  1. 展开创建新连接 => OLE DB(ADO) => 创建新连接。

如何在 Crystal Reports 中使用 C# 添加条形码:图 7 - 展开 创建新连接 - OLE DB(ADO) - 创建新连接。

  1. 选择"Microsoft OLE DB 数据源(适用于 SQL Server)",然后单击"下一步"。

如何在 Crystal Reports 中使用 C# 添加条形码:图 8 - 选择 SQL Server 的 Microsoft OLE DB 数据源,然后单击"下一步"。

  1. 请提供如下所示的服务器名称、登录凭据和数据库名称。

    ![如何使用 C# 在 Crystal Reports 中添加条形码:图 9 - 指定 SQL Server 名称、登录凭据和数据库名称。 然后点击"下一步",再点击"完成"。

  2. 按下"下一步"按钮,将出现一个新窗口,然后单击"完成"。
  3. 选择要添加的表格。 在这种情况下,请选择"产品"表。

    ![如何使用 C# 在 Crystal Reports 中添加条形码:图 10 - 添加表:从 ProductDB 数据库中选择 Products 表。 然后单击"确定"按钮。

  4. 点击确定按钮。

我们已建立数据库连接。 现在,我们来设置报表布局。

现在,我添加了一个文本框,内容为"Crystal Report C# 中的条形码"。 我添加了一个文本框,并将数据库字段中的产品 ID、产品名称、产品价格字段和产品条形码拖放到该文本框中,如下图所示。

如何使用 C# 在 Crystal Reports 中添加条形码:图 11 - 在 C# 中创建 Crystal Report。 从数据库字段中拖放产品 ID、产品名称、产品价格和产品条形码字段。

在 Crystal Report Viewer 中查看 Crystal Report 预览。

如何在 Crystal Reports 中使用 C# 添加条形码:图 12 - 在 Crystal Report Viewer 中检查 Crystal Report 预览。

构建并运行项目。 译文如下:

如何使用 C# 在 Crystal Reports 中添加条形码:图 13 - 输出:带有条形码的 Crystal Reports

我没有下载示例数据集包,也没有确保拥有正确的数据文件,而是使用 C# 创建了一个 Crystal Report 来进行全面的数据可视化。

这样,我们就可以在 Crystal Reports 应用程序中创建条形码,而无需下载条形码字体。 同样,我们也可以根据您的要求添加二维码。

结论

总之,使用 C# 将条形码集成到 Crystal Reports 中是增强数据表示和管理的一种有效方法。 选择像IronBarcode这样可靠的条形码库可以简化流程,提供多功能性和易用性。 IronBarcode 支持各种条形码类型、调整大小和自定义功能,证明其在条形码生成和读取任务中是一项宝贵的工具。 这里提供的分步指南可确保从选择库到设计 Crystal Report 布局的无缝实施过程。

此外,IronBarcode 还为开发者提供了解锁更多功能和支持的机会,以增强他们的条形码集成体验。 IronBarcode 的这种灵活性使其成为开发人员的理想选择,无论他们是在开发小型项目还是企业级应用程序。

常见问题解答

如何使用 C# 将条形码集成到 Crystal Reports 中?

要使用 C# 将条形码集成到 Crystal Reports 中,可以使用 IronBarcode 生成条形码图像。首先安装 IronBarcode 库,生成条形码图像,将其保存到数据库,然后使用 Crystal Reports 设计布局并合并条形码图像。

在 C# 中生成条形码的过程是什么?

使用 IronBarcode,可以通过创建条形码对象、使用调整大小和添加文本等选项进行自定义,然后将图像保存到文件或数据库中以供报告使用来生成 C# 中的条形码。

如何在 C# 中将条形码图像保存到数据库?

使用 IronBarcode 生成条形码图像后,可以通过将图像转换为二进制格式并使用 ADO.NET 将其插入数据库表来将其保存到数据库。

在 C# 中使用 IronBarcode 进行条形码生成的好处是什么?

IronBarcode 在 C# 中生成条形码很有帮助,因为它支持各种条形码格式,允许自定义,并提供一个简单的 API 可以快速集成到应用程序中,增强库存和文档跟踪。

如何在 Crystal Reports 中设置用于条形码集成的数据库连接?

要在 Crystal Reports 中设置数据库连接,使用字段资源管理器导航到数据库字段,然后进入数据库专家,展开创建一个新连接,选择 OLE DB(ADO),并使用必要的服务器详细信息配置您的连接。

可以使用 IronBarcode 在 Crystal Reports 中生成二维码吗?

是的,IronBarcode 可以用于生成二维码以及其他条形码类型,使其成为将不同条形码格式集成到 Crystal Reports 中的多功能选择。

设计带有条形码的 Crystal Report 布局应该遵循哪些步骤?

设计带有条形码的 Crystal Report 布局涉及创建报表模板,建立数据库连接,添加条形码图像字段,并格式化报告以有效合并条形码数据。

IronBarcode 如何在 C# 项目中增强报告功能?

IronBarcode 通过提供一种简单易用的解决方案生成和集成条形码来增强 C# 项目的报告能力,从而改善报告中的数据可视化和管理。

Jordi Bardia
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。