跳過到頁腳內容
使用IRONBARCODE

如何使用C#在Crystal Reports中添加條碼

Crystal Reports 是一個強大的報告工具,允許開發人員為其應用程式創建功能豐富的報告。 When it comes to including barcodes in Crystal Reports using C#, it adds a new dimension to data representation, making it easier to manage and track information. 在本文中,我們將探討使用 C# 在 Crystal Reports 中整合條碼的步驟。

如何在 Crystal Reports 中使用 C&數入條碼

  1. 安裝條碼庫。
  2. 生成條碼圖像並將其保存為資料庫表中的一個圖像。
  3. 設計 Crystal Report 的佈局。
  4. 建立與資料庫的連接並選擇必要的表。
  5. 在 Crystal Report 佈局中包含一個條碼圖像欄位。
  6. 構建並運行項目。

選擇條碼庫

在開始實施之前,選擇一個支持 Crystal Reports 和 C# 的條碼庫是至關重要的。 一個熱門的選擇是針對 .NET 的 IronBarcode

什麼是 IronBarcode

IronBarcode is a versatile .NET library that simplifies barcode generation and 閱讀。 使用 IronBarcode,您可以輕鬆創建包括 Code 128 和 QR 碼在內的各種條碼,只需指定要編碼的值即可。 It also supports resizing and customization. 在閱讀方面,IronBarcode 可以從圖像或 PDF 中提取條碼數據,非常適合庫存管理和文檔跟踪。 其用戶友好的 API 保證了在您的項目中快速集成,跨平台支持允許在不同 .NET 版本中無縫開發。 無論您是經驗豐富的開發人員還是初學者,IronBarcode 都可以有效地支持您使用條碼。

創建一個新項目

打開 Visual Studio 來創建 ASP.NET Crystal Reports 網站。我使用的是 Visual Studio 2022。 您可以使用任何版本,但請確保為該特定版本安裝了 Crystal reports for Visual Studio。

如何在 Crystal Reports 中使用 C#: 圖 1 - 打開 Visual Studio。 創建一個新的 ASP.NET Crystal Reports Web Site項目。

選擇項目名稱,位置和目標框架。 點擊創建按鈕。 將會創建一個新項目,如下所示。

如何在 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 套件管理器控制台。 您可以通過轉到查看 -> 其他窗口 -> 套件管理器控制台來找到它。

如何在 Crystal Reports 中使用 C#: 圖 3 - 打開 NuGet 套件管理器控制台。 導航到查看菜單 - 其他窗口 - 套件管理器控制台

在套件管理器控制台中,使用以下命令安裝 IronBarcode 庫:

Install-Package BarCode

按回車鍵以執行命令。

或者,您可以使用解決方案的 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()

    • 這個函數在條碼下方添加條碼值的描述文本。
  3. ResizeTo(600, 300)

    • 調整條碼圖像的高度和寬度到所提供的尺寸。
  4. 您可以使用 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),以及身份驗證憑證(用戶ID sa密碼 123456)。

3. SQL 命令創建

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

    • 創建一個新的 SqlCommand 對象。 此命令表示一個 SQL 查詢以將數據插入到產品表中。
    • 該查詢向表列插入值:77446252'Pine Apple Small''100',以及生成的條碼的二進制流(myBarcode.BinaryStream)。

4. 資料庫交互

  1. cn.Open();: 資料庫連接被打開。
  2. cmd.ExecuteNonQuery();: 執行 SQL 查詢,將指定的值插入到產品表中。
  3. cn.Close();: 關閉資料庫連接以釋放資源。

設計您的 Crystal Reports 條碼生成器

現在,設計報告佈局,添加資料庫連接,並排列必要的欄位。 如果您是新手,請按照以下步驟操作。

  1. 打開欄位瀏覽器 => 資料庫欄位 => 資料庫專家。

    如何在 Crystal Reports 中使用 C#: 圖 6 - 添加資料庫連接到報告佈局:打開欄位瀏覽器 - 資料庫欄位 - 資料庫專家。

  2. 展開創建新連接 => OLE DB(ADO) => 創建新連接。

    如何在 Crystal Reports 中使用 C#: 圖 7 - 展開創建新連接 - OLE DB(ADO) - 創建新連接。

  3. 選擇適用於 SQL Server 的 Microsoft OLE DB 數據源,然後點擊下一步。

    如何在 Crystal Reports 中使用 C#: 圖 8 - 選擇適用於 SQL Server 的 Microsoft OLE DB 數據源,然後點擊下一步。

  4. 提供服務器名稱,登入憑證和資料庫名稱,如下所示。

    如何在 Crystal Reports 中使用 C#: 圖 9 - 指定 SQL 服務器名稱,登入憑證和資料庫名稱。</S 然後點擊下一步 - 完成。

  5. 按下一步按鈕,將出現一個新窗口,然後點擊完成。
  6. 選擇要添加的表。 在這種情況下,選擇產品表。

    如何在 Crystal Reports 中使用 C#: 圖 10 - 添加表:從 ProductDB 資料庫中選擇產品表。 然後點擊確定按鈕。

  7. 點擊確定按鈕。

我們已經設定了一個資料庫連接。 現在,讓我們設置報告佈局。

現在,我已經添加了一個文本框,文本顯示為"條碼在 Crystal report C# 中"。 我添加了一個文本框,然後拖放產品 ID、產品名稱、產品價格欄位和產品條碼從資料庫欄位,並將它們放置在框中,如下圖所示。

如何在 Crystal Reports 中使用 C#: 圖 11 - 創建C#中的Crystal報告。 從資料庫欄位拖放產品 ID、產品名稱、產品價格、產品條碼欄位。

查看 Crystal Report Viewer 中的 Crystal Report 預覽。

如何在 Crystal Reports 中使用 C#: 圖 12 - 在 Crystal Report Viewer 中查看 Crystal Report 預覽。

構建並運行項目。 輸出如下:

如何在 Crystal Reports 中使用 C#: 圖 13 - 輸出:帶有條碼的 Crystal Report

我還沒有下載示例數據集包,以確保我擁有正確的數據文件,並著手在 C# 中創建一個 Crystal 報告,實現全面的數據可視化。

通過這種方式,我們可以在Crystal Reports應用中創建條碼,而不需要下載條碼字體。 類似地,我們也可以根據您的需求添加二維碼。

結論

總之,將條碼集成到 Crystal Reports 中使用 C# 是一種增強數據表示和管理的強大方式。 選擇一個可靠的條碼庫,例如 IronBarcode,可以簡化過程,提供靈活性和易用性。 IronBarcode,憑藉其支持各種條碼類型、尺寸調整和自定義功能,證明在條碼生成和閱讀任務中是一個有價值的資產。 本文提供的逐步指南確保了一個無縫的實施過程,從選擇庫到設計 Crystal Report 佈局。

此外,IronBarcode 提供了開發者在條碼集成過程中解鎖附加功能和支持的機會。 這一靈活性使 IronBarcode 成為開發人員有吸引力的選擇,無論他們正在進行小型項目或企業級應用。

常見問題解答

我怎樣才能使用 C# 在 Crystal Reports 中整合條碼?

要使用 C# 在 Crystal Reports 中整合條碼,可以使用 IronBarcode 生成條碼圖像。首先,安裝 IronBarcode 庫,生成條碼圖像,將其保存到資料庫,然後使用 Crystal Reports 設計佈局並納入條碼圖像。

生成條碼的 C# 處理流程是什麼?

使用 IronBarcode,可以透過創建條碼對象生成條碼,並用如調整大小和添加文字等選項自定義它,然後將圖像保存到文件或資料庫以用於報告中。

您如何在 C# 中將條碼圖像保存到資料庫?

在使用 IronBarcode 生成條碼圖像後,可以將其轉換為二進制格式,並使用 ADO.NET 將其插入到資料庫表中。

使用 IronBarcode 進行條碼生成的好處是什麼?

IronBarcode 在 C# 中生成條碼具有多種條碼格式支援、可自定義性,並提供簡單的 API 以便快速整合到應用程序中,從而增強了庫存和文檔追蹤。

我如何設定 Crystal Reports 中的資料庫連接以進行條碼整合?

要在 Crystal Reports 中設定資料庫連接,使用 Field Explorer 導向到 Database Field,然後進入 Database Expert,展開 Create a new Connection,選擇 OLE DB(ADO),並使用必要的伺服器詳細信息配置您的連接。

IronBarcode 可以用於在 Crystal Reports 中生成 QR 碼嗎?

是的,IronBarcode 可以用於生成 QR 碼以及其他條碼類型,使其成為將各種條碼格式整合到 Crystal Reports 中的多功能選擇。

設計帶有條碼的 Crystal Report 佈局應遵循哪些步驟?

設計帶有條碼的 Crystal Report 佈局涉及創建報表模板,建立資料庫連接,添加條碼圖像欄位,並格式化報告以有效地納入條碼數據。

IronBarcode 如何增強 C# 項目的報告能力?

IronBarcode 通過提供靈活且易於使用的解決方案來生成和整合條碼,從而改進報告中的數據可視化和管理,增強了 C# 項目的報告能力。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。