IRONSOFTWAREHOME
更新 Excel 資料庫記錄
using IronXL;
using System.Data;
using System.Data.SqlClient;

// Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Convert the workbook to ToDataSet
DataSet dataSet = workBook.ToDataSet();

// Your sql query
string sql = "SELECT * FROM Users";

// Your connection string
string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=usersdb;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    // Open connections to the database
    connection.Open();
    SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);

    // Update the values in database using the values in Excel
    adapter.Update(dataSet);
}
Imports IronXL
Imports System.Data
Imports System.Data.SqlClient

' Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Convert the workbook to ToDataSet
Dim dataSet As DataSet = workBook.ToDataSet()

' Your sql query
Dim sql As String = "SELECT * FROM Users"

' Your connection string
Dim connectionString As String = "Data Source=.\SQLEXPRESS;Initial Catalog=usersdb;Integrated Security=True"
Using connection As New SqlConnection(connectionString)
    ' Open connections to the database
    connection.Open()
    Dim adapter As New SqlDataAdapter(sql, connection)

    ' Update the values in database using the values in Excel
    adapter.Update(dataSet)
End Using
NuGet Download
PM > Install-Package IronXL.Excel
更新 Excel 資料庫記錄

更新 Excel 資料庫記錄

IronXL 的 ToDataSet 方法允許開發者輕鬆從 Excel 資料同步和更新 SQL 資料庫記錄。 此功能通過將 Excel 工作簿轉換為 DataSet,實現了 Excel 表格式資料與關聯式資料庫之間的無縫整合。 它消除了手動資料輸入或複雜轉換的需求,顯著簡化了資料管理工作流程。

此功能對於需要定期從外部 Excel 文件更新資料庫記錄的應用程式特別有用,例如客戶資料、財務記錄或庫存管理系統。 與 SQL 查詢和適配器的相容性允許開發人員將 Excel 資料映射到現有資料庫架構。 IronXL 支援多種 Excel 格式 (XLSX、CSV 等),確保在各種使用案例中具有多樣性。 通過利用 SqlDataAdapter 進行批量更新,此功能減少了資料處理時間,減少錯誤,並確保 Excel 和資料庫記錄之間的一致性,使其成為企業級應用程式的理想解決方案。

此程式碼演示如何使用 IronXL 和 ADO.NET 從 Excel 文件更新 SQL 資料庫的過程。 WorkBook.Load 方法將名為 Products.xlsx 的 Excel 文件載入到記憶體中,ToDataSet 方法將工作簿轉換為 DataSet,其將資料組織成與 ADO.NET 相容的表格式結構。

設定了一個 SQL 查詢以與資料庫中的特定表進行互動 (products)。 SqlDataAdapter 被初始化與查詢和連接字串,以使其能在 DataSetdatabase 之間擔任橋梁角色。 SqlCommandBuilder 用於自動生成必要的 SQL 命令,以便在資料庫中插入、更新或刪除資料。 最後,adapter.Update(dataSet) 方法同步 DataSet (從 Excel 文件填充) 的更改到相應的資料庫表。 這簡化了資料傳輸過程並確保資料庫反映 Excel 文件的最新更改。

探索如何使用 IronXL 將 DataSets 匯出到 Excel
在GitHub上查看

準備好開始了嗎?

Nuget Downloads 2,237,574版本:2026.9剛剛發布

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費即時演示
Booking Badge

全球數百萬工程師的信賴

Iron Software的客戶標誌
獲得無義務諮詢
填寫以下表格或電郵sales@ironsoftware.com
您的資料將始終保密。
全球數百萬工程師的信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立