如何使用C#在Crystal Reports中添加條碼
Crystal Reports是一款功能強大的報表工具,它允許開發人員為其應用程式建立功能豐富的報表。 在 Crystal Reports 中使用C#新增條碼時,它為資料表示增添了一個新的維度,使資訊更容易管理和追蹤。 在本文中,我們將探討使用 C# 將條碼整合到 Crystal Reports 中的步驟。
如何使用 C# 在 Crystal Reports 中新增條碼
- 安裝條碼庫。
- 產生條碼圖像並將其作為圖像儲存到資料庫表中。
- 設計 Crystal Report 的版面。
- 建立與資料庫的連線並選擇必要的表格。
- 在 Crystal Report 版面配置中新增條碼影像欄位。
- 建置並運行專案。
選擇條碼庫
在深入實施之前,選擇一個支援 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 套件管理器控制台。 您可以依序點擊"檢視"->"其他視窗"->"套件管理器控制台"來找到它。

在軟體包管理器控制台中,使用以下命令安裝 IronBarcode 庫:
Install-Package BarCode
按回車鍵執行命令。
或者,您可以使用"管理解決方案的 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程式碼解釋
1. 條碼生成
var myBarcode = BarcodeWriter.CreateBarcode("77446252", BarcodeWriterEncoding.Code128);- 此行使用
BarcodeWriter類別的CreateBarcode()方法建立條碼。 條碼是根據字串"77446252"表示的數據,採用 Code 128 編碼格式產生的。myBarcode變數現在保存著產生的條碼。
- 此行使用
2.AddBarcodeValueTextBelowBarcode AddBarcodeValueTextBelowBarcode()
- 此功能會在條碼下方新增條碼值的描述文字。ResizeTo(600, 300)- 將條碼影像的高度和寬度調整為提供的尺寸。
- 您可以使用
SaveAs()方法將條碼影像儲存到檔案系統中。
上述程式碼將產生以下條碼:
如何在 Crystal Reports 中使用 C# 新增條碼:圖 5 - 輸出:產生的條碼
2. 資料庫連線設定
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 sa和password 123456)。
- 建立與 SQL Server 資料庫的連線。 連線字串指定伺服器(
3. SQL 命令創建
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. 資料庫交互
- cn.Open(); : 資料庫連線已開啟。
- cmd.ExecuteNonQuery(); : 執行 SQL 查詢,將指定的值插入 Products 表中。
- cn.Close(); : 關閉資料庫連線以釋放資源。
設計您的 Crystal Reports 條碼產生器
現在,設計報表佈局,新增資料庫連接,並安排必要的欄位。 如果你是新手,請按照以下步驟操作。
開啟欄位資源管理器 => 資料庫欄位 => 資料庫專家。
如何在 Crystal Reports 中使用 C# 新增條碼:圖 6 - 將資料庫連線新增至報表版面配置:開啟欄位資源管理器 - 資料庫欄位 - 資料庫專家。
展開建立新連線 => OLE DB(ADO) => 建立新連線。
如何在 Crystal Reports 中使用 C# 新增條碼:圖 7 - 展開 建立新連線 - OLE DB(ADO) - 建立新連線。
選擇"Microsoft OLE DB 資料來源(適用於 SQL Server)",然後按一下"下一步"。
如何在 Crystal Reports 中使用 C# 新增條碼:圖 8 - 選擇 SQL Server 的 Microsoft OLE DB 資料來源,然後按一下"下一步"。
請提供如下所示的伺服器名稱、登入憑證和資料庫名稱。

在 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# 將 BarCode 整合到 Crystal Reports 中?
若要使用 C# 將條碼整合至 Crystal Reports,您可以使用 IronBarcode 來產生條碼影像。首先,安裝 IronBarcode 函式庫,產生條碼影像,將其儲存至資料庫,然後再使用 Crystal Reports 設計版面並整合條碼影像。
在 C# 中生成 BarCode 的流程是什麼?
使用 IronBarcode,您可以在 C# 中生成條碼,方法是創建一個條碼對象,使用調整大小和添加文本等選項對其進行定制,然後將圖像保存到文件或資料庫,以便在報告中使用。
如何在 C# 中將 BarCode 影像儲存至資料庫?
使用 IronBarcode for .NET 生成条形码图像后,您可以通过将图像转换为二进制格式并使用 ADO.NET 将其插入数据库表来将其保存到数据库中。
在 C# 中使用 IronBarcode 生成条码有哪些好处?
IronBarcode 對於在 C# 中產生條碼是有益的,因為它支援各種條碼格式,允許自訂,並提供簡單的 API 以快速整合到應用程式中,加強庫存和文件追蹤。
如何在 Crystal Reports 中為條碼整合設定資料庫連線?
若要在 Crystal Reports 中設定資料庫連線,請使用欄位資源總管導航至資料庫欄位,然後轉至資料庫專家,展開建立新連線,選擇 OLE DB(ADO),並使用必要的伺服器詳細資訊設定您的連線。
IronBarcode 可以用來在 Crystal Reports 中產生 QR 碼嗎?
是的,IronBarcode 可用於產生 QR 代碼,以及其他條碼類型,使其成為將不同條碼格式整合至 Crystal Reports 的多功能選擇。
設計具有 BarCode 的 Crystal Report 佈局應遵循哪些步驟?
設計具有條碼的 Crystal Report 佈局包括建立報表範本、建立資料庫連線、新增條碼影像欄位,以及格式化報表以有效整合條碼資料。
IronBarcode 如何增強 C# 專案的報表功能?
IronBarcode 通過提供靈活易用的解決方案來生成和整合 BarCode,從而增強了 C# 專案中的報表功能,改善了報表中的數據可視化和管理。









