跳過到頁腳內容
使用IRONBARCODE

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

Crystal Reports 是一款強大的報表工具,能讓開發者為其應用程式創建功能豐富的報表。 當涉及到在 條碼 中使用 C# 在 Crystal Reports 中時,為數據表示添加了一個新的層次,讓信息更易於管理和跟踪。 在本文中,我們將探討使用 C# 將條碼整合到 Crystal Reports 中的步驟。

How to Add a Barcode in Crystal Reports using C

  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。

如何在 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 程式包管理器主控台。 您可以通過轉到 View -> Other Windows -> Package Manager Console 來找到它。

如何在 Crystal Reports 中使用 C# 添加條碼:圖 3 - 打開 NuGet 程式包管理器主控台。 導航到 View 菜單 - Other Windows - Package Manager Console

在 Package Manager Console 中,使用以下命令來安裝 IronBarcode 程式庫:

Install-Package BarCode

按 Enter 鍵執行該命令。

或者,您可以通過為方案管理 NuGet 程式包來安裝 IronBarcode 程式庫:

How to Add a Barcode in Crystal Reports using C#: Figure 4 - Install IronBarcode using the Manage NuGet Package for Solution by searching IronBarcode in the search bar of NuGet Package Manager, then select the project and click on the Install button.

等待 NuGet Package Manager 下載並安裝 IronBarcode 程式庫及其相依性。 安裝完成後,您會在 Package Manager Console 中看到一條確認訊息。

現在,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();
        }
    }
}
$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 數據庫的連接。 連接字符串指定了伺服器 (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 查詢。
    • 查詢將值插入到表列中:'100',以及生成的條碼的二進制流 (myBarcode.BinaryStream)。

4. 數據庫交互

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

設計您的 Crystal Reports 條碼生成器

現在,設計報表佈局,添加數據庫連接,並安排必要的字段。 如果您是新手,請遵循以下步驟。

  1. 打開 Field Explorer => Database Field => Database Expert。

    如何在 Crystal Reports 中使用 C# 添加條碼:圖 6 - 向報表佈局中添加數據庫連接:打開 Field Explorer - Database Field - Database Expert。

  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 伺服器名稱、登錄憑證和數據庫名稱。 然後點擊下一步 - 然後完成。

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

    如何在 Crystal Reports 中使用 C# 添加條碼:圖 10 - 添加表:從 ProductDB 數據庫中選擇 Products 表。 然後點擊確定按鈕。

  7. 點擊確定按鈕。

我們已經設置了數據庫連接。 現在,讓我們設置報表佈局。

現在,我添加了一個文本框,內含 "Barcode in Crystal report C#" 文本。 我添加了一個文本框,將產品 ID、產品名稱、產品價格字段和產品條碼從數據庫字段中拖放進框中,並如下面所示安排它們。

如何在 Crystal Reports 中使用 C# 添加條碼:圖 11 - 在 C# 中創建一個 Crystal Report。 從數據庫字段中拖放產品 ID、產品名稱、產品價格、產品條碼字段。

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

如何在 Crystal Reports 中使用 C# 添加條碼:圖 12 - 在 Crystal Report Viewer 中檢查 Crystal Report 預覽。

構建並運行專案。 輸出如下所示:

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

我尚未下載示例數據集包,確保手頭有正確的數據文件,然後繼續在 C# 中創建一個 Crystal Report,用於全面數據可視化。

通過這種方式,我們可以在 Crystal Reports 應用程式中創建條碼,而不需下載條碼字體。 同樣,我們也可以根據您的需求添加 QR 碼。

結論

總之,使用 C# 將條碼整合到 Crystal Reports 中,是增強數據表示和管理的一種強大方式。 選擇像 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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我