跳至頁尾內容
使用IRONBARCODE
如何使用C#將條碼新增到Crystal Reports | IronBarcode

如何使用C#在Crystal Reports中新增條碼

Crystal Reports是一款強大的報表工具,允許開發者為他們的應用程式建立功能豐富的報表。 在使用條碼包含在Crystal Reports中的C#時,它為資料表示新增了一個新維度,使資訊的管理和跟踪更加容易。 在本文中,我們將探討使用C#將條碼整合到Crystal Reports中的步驟。

How to Add a Barcode in Crystal Reports using C

  1. 安裝條碼程式庫。
  2. 生成條碼圖像並將其作為圖像儲存在資料庫表中。
  3. 設計Crystal Reports的版面。
  4. 建立與資料庫的連接並選擇必要的表格。
  5. 在Crystal Reports版面中包括一個條碼圖像字段。
  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網站項目。

選擇項目名稱、位置和目標框架。 點擊建立按鈕。 將建立如下面所示的項目。

如何使用C#在Crystal Reports中新增條碼:圖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 Package管理器安裝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套件管理器下載並安裝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);

    • 這行程式碼使用CreateBarcode()方法建立一個條碼。 該條碼由串"77446252"所表示的資料使用Code 128編碼格式生成。 myBarcode變數現在保存著生成的條碼。
  2. AddBarcodeValueTextBelowBarcode()

    • 該功能在條碼下面新增了條碼值的描述文字。
  3. ResizeTo(600, 300)

    • 根據提供的尺寸調整條碼圖像的高度和寬度。
  4. 您可以使用SaveAs()方法將條碼圖像儲存在文件系統中。

將從上面的程式碼生成的條碼如下所示:

如何使用C#在Crystal Reports中新增條碼:圖5 - 輸出:生成的條碼

2. 資料庫連接設置

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

    • 建立與SQL Server資料庫的連接。 連接字串規定了伺服器(password 123456)。

3. SQL命令建立

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

    • 建立一個新的SqlCommand物件。 該命令表示向Products表插入資料的SQL查詢。
    • 該查詢向表列插入數值:myBarcode.BinaryStream)。

4. 資料庫互動

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

設計您的Crystal Reports條碼生成器

現在,設計報告版面,新增資料庫連接並安排必要的字段。 如果您是新手,請遵循以下步驟。

  1. 打開字段瀏覽器 => 資料庫字段 => 資料庫專家。

    如何使用C#在Crystal Reports中新增條碼:圖6 - 為報告版面新增資料庫連接:打開字段瀏覽器 - 資料庫字段 - 資料庫專家。

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

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

  3. 選擇Microsoft OLE DB資料來源為SQL Server,然後點擊下一步。

    如何使用C#在Crystal Reports中新增條碼:圖8 - 選擇Microsoft OLE DB資料來源為SQL Server並點擊下一步。

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

    如何使用C#在Crystal Reports中新增條碼:圖9 - 指定SQL Server名稱、登錄憑證和資料庫名稱。 然後點擊下一步 - 然後完成。

  5. 按下一步按鈕,出現一個新視窗,然後點擊完成。
  6. 選擇您要新增的表。 在本例中,選擇Products表。

    如何使用C#在Crystal Reports中新增條碼:圖10 - 新增表:從ProductDB資料庫選擇Products表。 然後點擊確認按鈕。

  7. 點擊確認按鈕。

我們已設置資料庫連接。 現在,讓我們設置報告版面。

現在,我已經新增了一個包含"Barcode in Crystal report C#"文字的文字框。 我新增了一個文字框,並將產品ID、產品名稱、產品價格字段和產品條碼從資料庫字段中拖放到框內,如下所示。

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

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

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

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

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

我沒有下載範例資料集包,確保我擁有正確的資料文件,然後著手在C#中建立一個完整的資料可視化Crystal報告。

這樣,我們可以在Crystal Reports應用程式中建立條碼,而無需下載條碼字型。 同樣,我們也可以根據您的需求新增QR Code。

結論

總之,使用C#將條碼整合到Crystal Reports中是一種增強資料表示和管理的強大方法。 選擇可靠的條碼程式庫,比如IronBarcode可以簡化過程,提供多樣性和易用性。 IronBarcode,憑藉其對各種條碼型別的支援、調整大小和自定功能,在條碼生成和閱讀任務中被證明是一種有價值的資產。 這裡提供的分步指南確保了一個無障礙的實施過程,從選擇程式庫到設計Crystal Reports版面。

此外,IronBarcode為開發者提供了解鎖額外功能和支援以增強其條碼整合體驗的機會。 這種靈活性使得IronBarcode成為開發者的有力選擇,無論他們是在處理小型專案還是企業級應用程式。

常見問題

如何在Crystal Reports中使用C#整合條碼?

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

生成C#條碼的過程是什麼?

使用IronBarcode,可以通過建立條碼物件、使用像調整大小和增加文字等選項進行自定義,然後將圖像保存到文件或資料庫中,以用於報告。

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

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

使用IronBarcode進行C#條碼生成有哪些好處?

IronBarcode對於C#的條碼生成有益,因為它支持多種條碼格式,允許自定義,並提供簡單的API以快速整合到應用程式中,增強了庫存和文件跟蹤。

如何在Crystal Reports中設置條碼整合的資料庫連接?

要在Crystal Reports中設置資料庫連接,請使用字段資源管理器導航到資料庫字段,然後轉到資料庫專家,展開建立新連接,選擇OLE DB(ADO),並使用必要的伺服器詳細資訊配置您的連接。

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

是的,IronBarcode可以用於生成QR碼以及其他條碼型別,使其成為整合不同條碼格式進入Crystal Reports的多功能選擇。

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

設計帶有條碼的Crystal Report佈局需要建立報告範本,建立資料庫連接,新增條碼圖像字段,並將報告格式化以有效地整合條碼資料。

IronBarcode如何增強C#專案的報告功能?

IronBarcode通過提供靈活且易用的條碼生成和整合解決方案來增強C#專案的報告功能,這改善了報告中的資料可視化和管理。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

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