使用 IRONXL 如何从 CSV 文件中读取数据并将其存储在数据库 C# 中 Curtis Chau 已更新:七月 28, 2025 Download IronXL NuGet 下载 DLL 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article In today's digital world, handling data efficiently is an important task. One common requirement in software development is reading data from a CSV file and storing it in a database. This tutorial covers the steps to read data from a CSV file and store it in a SQL Server database using C#, specifically utilizing the IronXL library. This guide is designed for beginners and will be explained in a simple, engaging way. Understanding the Basics What is a CSV File? A CSV (Comma-Separated Values) file is a plain text file that contains data separated by commas. It's a popular format for transferring data due to its simplicity and compatibility with various applications, such as Excel. The Role of SQL Server and Databases SQL Server is a database management system by Microsoft. It's used to store and manage data in a structured way. In our case, we'll store the CSV data in a SQL Server table. Introduction to IronXL IronXL is an Excel library tailored for .NET applications, specifically designed to enable developers to read, generate, and edit Excel files without needing the Microsoft Office Interop. This library stands out for its compatibility with various .NET versions and platforms, including .NET Core, .NET Standard, and .NET Framework, as well as its support for different operating systems like Windows, Linux, and macOS. It's a powerful library for importing data, especially for handling CSV files. How to Read and Store Data From CSV Files in C# Create a C# console program in Visual Studio. Install the CSV library using NuGet Package Manager. Load the CSV file in the program using the library. Create a connection with the database. Read content from the CSV file using the library. Copy that content into the database using the SqlBulkCopy method. Here's an example code snippet: using System; using System.Data; using System.Data.SqlClient; using System.Globalization; using System.IO; using CsvHelper; namespace CsvReader { class Program { static void Main(string[] args) { string csvPath = @"path\to\your\csv\file.csv"; using (var reader = new StreamReader(csvPath)) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { // Mapping the CSV data to the corresponding model var records = csv.GetRecords<YourModel>(); using (var sqlBulkCopy = new SqlBulkCopy("your_connection_string")) { sqlBulkCopy.DestinationTableName = "YourTableName"; sqlBulkCopy.WriteToServer(records.AsDataReader()); } Console.WriteLine("Data imported successfully!"); } } } // Define your model that maps to the CSV columns public class YourModel { // Define properties here representing the CSV columns } } using System; using System.Data; using System.Data.SqlClient; using System.Globalization; using System.IO; using CsvHelper; namespace CsvReader { class Program { static void Main(string[] args) { string csvPath = @"path\to\your\csv\file.csv"; using (var reader = new StreamReader(csvPath)) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { // Mapping the CSV data to the corresponding model var records = csv.GetRecords<YourModel>(); using (var sqlBulkCopy = new SqlBulkCopy("your_connection_string")) { sqlBulkCopy.DestinationTableName = "YourTableName"; sqlBulkCopy.WriteToServer(records.AsDataReader()); } Console.WriteLine("Data imported successfully!"); } } } // Define your model that maps to the CSV columns public class YourModel { // Define properties here representing the CSV columns } } Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Globalization Imports System.IO Imports CsvHelper Namespace CsvReader Friend Class Program Shared Sub Main(ByVal args() As String) Dim csvPath As String = "path\to\your\csv\file.csv" Using reader = New StreamReader(csvPath) Using csv = New CsvReader(reader, CultureInfo.InvariantCulture) ' Mapping the CSV data to the corresponding model Dim records = csv.GetRecords(Of YourModel)() Using sqlBulkCopy As New SqlBulkCopy("your_connection_string") sqlBulkCopy.DestinationTableName = "YourTableName" sqlBulkCopy.WriteToServer(records.AsDataReader()) End Using Console.WriteLine("Data imported successfully!") End Using End Using End Sub End Class ' Define your model that maps to the CSV columns Public Class YourModel ' Define properties here representing the CSV columns End Class End Namespace $vbLabelText $csharpLabel Make sure to replace "path\to\your\csv\file.csv" with the actual path to your CSV file, YourModel with the model representing your CSV data, "your_connection_string" with your database connection string, and "YourTableName" with the name of your database table. Setting Up the Environment Prerequisites Visual Studio: Ensure you have Visual Studio installed. SQL Server: You should have SQL Server installed and accessible. IronXL Installation: Install IronXL by running the following NuGet command: dotnet add package IronXL Make sure to run these commands within the project directory where you want to install IronXL. Creating a SQL Server Table Before importing data, create a destination table in your SQL Server database. This table will store the CSV data. CREATE TABLE YourTableName ( Column1 DataType, Column2 DataType, ... ); Replace YourTableName, Column1, Column2, and DataType with your specific details. Step-by-Step Guide to Import CSV Data First, make sure you have a CSV file containing the data you want to import. Create a new C# console application project in Visual Studio. Install the CsvHelper NuGet package for reading CSV files. You can do this by opening the NuGet Package Manager Console in Visual Studio and running the following command: Install-Package CsvHelper Install-Package CsvHelper SHELL Add the necessary using statements at the top of your C# code file: using System; using System.IO; using System.Globalization; using CsvHelper; using System.Data.SqlClient; using System; using System.IO; using System.Globalization; using CsvHelper; using System.Data.SqlClient; Imports System Imports System.IO Imports System.Globalization Imports CsvHelper Imports System.Data.SqlClient $vbLabelText $csharpLabel In your code, specify the path to your CSV file. For example: string csvFilePath = @"C:\path\to\your\csv\file.csv"; string csvFilePath = @"C:\path\to\your\csv\file.csv"; Dim csvFilePath As String = "C:\path\to\your\csv\file.csv" $vbLabelText $csharpLabel Make sure to replace C:\path\to\your\csv\file.csv with the actual path to your CSV file. Create a new instance of the StreamReader class to read the CSV file: using (var reader = new StreamReader(csvFilePath)) { // code goes here } using (var reader = new StreamReader(csvFilePath)) { // code goes here } Using reader = New StreamReader(csvFilePath) ' code goes here End Using $vbLabelText $csharpLabel Create a new instance of the CsvReader class, passing in the StreamReader object: using (var reader = new StreamReader(csvFilePath)) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { // code goes here } using (var reader = new StreamReader(csvFilePath)) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { // code goes here } Using reader = New StreamReader(csvFilePath) Using csv = New CsvReader(reader, CultureInfo.InvariantCulture) ' code goes here End Using End Using $vbLabelText $csharpLabel Optionally, configure the CsvReader object with any necessary settings. For example: csv.Configuration.HasHeaderRecord = true; csv.Configuration.HasHeaderRecord = true; csv.Configuration.HasHeaderRecord = True $vbLabelText $csharpLabel Use the GetRecords<T>() method of the CsvReader object to read the CSV data into a collection of objects. Replace <T> with the type of object that represents each record in the CSV file. For example: var records = csv.GetRecords<YourModel>(); var records = csv.GetRecords<YourModel>(); Dim records = csv.GetRecords(Of YourModel)() $vbLabelText $csharpLabel Make sure to replace YourModel with the actual name of your model class. Iterate over the records and perform any required processing or validation: foreach (var record in records) { // Process each record as needed } foreach (var record in records) { // Process each record as needed } For Each record In records ' Process each record as needed Next record $vbLabelText $csharpLabel Optionally, establish a connection to your SQL Server database using ADO.NET or an ORM tool like Entity Framework. Insert each record into the database using your chosen database access mechanism. For example, if you're using ADO.NET, you can leverage the SqlBulkCopy class to efficiently bulk insert the data. Handle any exceptions that may occur during the import process and provide appropriate error messages or logging. Test your application by running it and verifying that the CSV data is successfully imported into your database. That's it! You've now successfully imported CSV data into your SQL Server database using C#. Step 1: Reading the CSV File When beginning the task of importing data from a CSV file, the first crucial step is to read the data within it accurately. Each line in a CSV file typically represents a data record, and each record consists of one or more fields, separated by commas. We then employ the IronXL library to handle the CSV file. To read the CSV file using IronXL, you'll use its WorkBook and WorkSheet classes. The WorkBook class represents an entire spreadsheet, which could be an Excel file or, in our case, a CSV file. When you load the CSV file path into a WorkBook object, IronXL treats the CSV file as a spreadsheet/data table. using IronXL; using System.Data; public class CSVReader { // Reads a CSV file and converts it to a DataTable public DataTable ReadCSV(string filePath) { WorkBook workbook = WorkBook.Load(filePath); WorkSheet sheet = workbook.DefaultWorkSheet; // Convert to DataTable for easier processing DataTable dataTable = sheet.ToDataTable(true); // Set to 'true' if your CSV has a header row return dataTable; } } using IronXL; using System.Data; public class CSVReader { // Reads a CSV file and converts it to a DataTable public DataTable ReadCSV(string filePath) { WorkBook workbook = WorkBook.Load(filePath); WorkSheet sheet = workbook.DefaultWorkSheet; // Convert to DataTable for easier processing DataTable dataTable = sheet.ToDataTable(true); // Set to 'true' if your CSV has a header row return dataTable; } } Imports IronXL Imports System.Data Public Class CSVReader ' Reads a CSV file and converts it to a DataTable Public Function ReadCSV(ByVal filePath As String) As DataTable Dim workbook As WorkBook = WorkBook.Load(filePath) Dim sheet As WorkSheet = workbook.DefaultWorkSheet ' Convert to DataTable for easier processing Dim dataTable As DataTable = sheet.ToDataTable(True) ' Set to 'true' if your CSV has a header row Return dataTable End Function End Class $vbLabelText $csharpLabel Step 2: Establishing a Database Connection Establishing a connection to your SQL Server database is a fundamental step in the process of storing your CSV data. This step involves setting up a communication link between your application and the database server. A successful connection is crucial because, without it, transferring data to the database would be impossible. This step focuses on creating and opening a connection using a connection string in C#. The connection string is a vital component as it contains the information needed to establish the connection. It's like a key that unlocks the door to your database. using System.Data.SqlClient; public class DatabaseConnector { // Connection string to connect to the database private string connectionString = "your_connection_string_here"; public SqlConnection ConnectToDatabase() { SqlConnection connection = new SqlConnection(connectionString); connection.Open(); return connection; } } using System.Data.SqlClient; public class DatabaseConnector { // Connection string to connect to the database private string connectionString = "your_connection_string_here"; public SqlConnection ConnectToDatabase() { SqlConnection connection = new SqlConnection(connectionString); connection.Open(); return connection; } } Imports System.Data.SqlClient Public Class DatabaseConnector ' Connection string to connect to the database Private connectionString As String = "your_connection_string_here" Public Function ConnectToDatabase() As SqlConnection Dim connection As New SqlConnection(connectionString) connection.Open() Return connection End Function End Class $vbLabelText $csharpLabel The connectionString variable holds all the necessary details to connect to your SQL Server. It typically includes the server name, database name, user ID, and password. An example connection string looks like this: Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;. It's essential to replace these placeholders with your actual server details. Step 3: Storing Data in the Database Having established a connection with the SQL Server database, the next critical step is to store the CSV data in the database. This step involves transferring the data you've read and processed into your SQL Server table. It's a crucial part of the data handling process, as it involves actual data migration from a local file to a database server. In this step, we'll focus on how the CSV data, now stored in a DataTable, is transferred to the SQL Server database. We use a combination of C# and SQL Server features to accomplish this task efficiently. using System; using System.Data; using System.Data.SqlClient; public class DataImporter { public void ImportData(DataTable dataTable) { using (SqlConnection connection = new DatabaseConnector().ConnectToDatabase()) { // Check if the table exists and create it if it does not. string tableName = "CSVData"; // Use a valid SQL table name format string checkTable = $"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{tableName}') BEGIN "; string createTable = "CREATE TABLE " + tableName + " ("; for (int i = 0; i < dataTable.Columns.Count; i++) { createTable += $"[{dataTable.Columns[i].ColumnName}] NVARCHAR(MAX)"; if (i < dataTable.Columns.Count - 1) createTable += ", "; } createTable += ") END"; SqlCommand createTableCommand = new SqlCommand(checkTable + createTable, connection); createTableCommand.ExecuteNonQuery(); // Now we use SqlBulkCopy to import the data using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) { bulkCopy.DestinationTableName = tableName; try { bulkCopy.WriteToServer(dataTable); Console.WriteLine("Data imported successfully!"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } } using System; using System.Data; using System.Data.SqlClient; public class DataImporter { public void ImportData(DataTable dataTable) { using (SqlConnection connection = new DatabaseConnector().ConnectToDatabase()) { // Check if the table exists and create it if it does not. string tableName = "CSVData"; // Use a valid SQL table name format string checkTable = $"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{tableName}') BEGIN "; string createTable = "CREATE TABLE " + tableName + " ("; for (int i = 0; i < dataTable.Columns.Count; i++) { createTable += $"[{dataTable.Columns[i].ColumnName}] NVARCHAR(MAX)"; if (i < dataTable.Columns.Count - 1) createTable += ", "; } createTable += ") END"; SqlCommand createTableCommand = new SqlCommand(checkTable + createTable, connection); createTableCommand.ExecuteNonQuery(); // Now we use SqlBulkCopy to import the data using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) { bulkCopy.DestinationTableName = tableName; try { bulkCopy.WriteToServer(dataTable); Console.WriteLine("Data imported successfully!"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } } Imports System Imports System.Data Imports System.Data.SqlClient Public Class DataImporter Public Sub ImportData(ByVal dataTable As DataTable) Using connection As SqlConnection = (New DatabaseConnector()).ConnectToDatabase() ' Check if the table exists and create it if it does not. Dim tableName As String = "CSVData" ' Use a valid SQL table name format Dim checkTable As String = $"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{tableName}') BEGIN " Dim createTable As String = "CREATE TABLE " & tableName & " (" For i As Integer = 0 To dataTable.Columns.Count - 1 createTable &= $"[{dataTable.Columns(i).ColumnName}] NVARCHAR(MAX)" If i < dataTable.Columns.Count - 1 Then createTable &= ", " End If Next i createTable &= ") END" Dim createTableCommand As New SqlCommand(checkTable & createTable, connection) createTableCommand.ExecuteNonQuery() ' Now we use SqlBulkCopy to import the data Using bulkCopy As New SqlBulkCopy(connection) bulkCopy.DestinationTableName = tableName Try bulkCopy.WriteToServer(dataTable) Console.WriteLine("Data imported successfully!") Catch ex As Exception Console.WriteLine(ex.Message) End Try End Using End Using End Sub End Class $vbLabelText $csharpLabel It starts by opening a connection to the SQL Server database using the DatabaseConnector class, ensuring a pathway for data transactions. The method checks for the existence of the table named "CSVData" within the database. If the table is not found, it proceeds to create it. The table's schema is constructed based on the DataTable schema passed to the method, with all columns set to NVARCHAR(MAX) to accommodate any text data. This is a generic approach and may need refinement to match specific data types more closely. After that, an SQL command is formulated and executed to either verify the existence of the table or to create it. This ensures that the subsequent bulk copy operation has a destination table ready for data insertion. With the table ready, the SqlBulkCopy class is employed to transfer data from the DataTable directly into the SQL Server table. This operation is designed for high-performance bulk data transfers, making it suitable for handling large volumes of data efficiently. Step 4: Putting It All Together After diligently working through the earlier steps of reading CSV data, establishing a database connection, and preparing to transfer the data, we arrive at the final and crucial stage: integrating these individual components into a cohesive process. This integration is done in the Main method of your C# application, where everything comes together, enabling the actual execution of data import from the CSV file to the SQL Server database. class Program { static void Main(string[] args) { string filePath = "path_to_your_csv_file.csv"; CSVReader reader = new CSVReader(); DataTable dataTable = reader.ReadCSV(filePath); DataImporter importer = new DataImporter(); importer.ImportData(dataTable); Console.WriteLine("Data imported successfully!"); } } class Program { static void Main(string[] args) { string filePath = "path_to_your_csv_file.csv"; CSVReader reader = new CSVReader(); DataTable dataTable = reader.ReadCSV(filePath); DataImporter importer = new DataImporter(); importer.ImportData(dataTable); Console.WriteLine("Data imported successfully!"); } } Friend Class Program Shared Sub Main(ByVal args() As String) Dim filePath As String = "path_to_your_csv_file.csv" Dim reader As New CSVReader() Dim dataTable As DataTable = reader.ReadCSV(filePath) Dim importer As New DataImporter() importer.ImportData(dataTable) Console.WriteLine("Data imported successfully!") End Sub End Class $vbLabelText $csharpLabel Replace path_to_your_csv_file.csv with the path to your CSV file. Running the Project Once you run the project, you'll see the following output. The success messages represent that all operations have been executed successfully and data is copied to the database. Data imported successfully! Now, you can open SQL Server Management Studio (SSMS) and check the table under the database. You'll see the following data in the table. Conclusion This tutorial has guided you through the process of reading data from a CSV file and storing it in a SQL Server database using C#. By following these steps and utilizing the IronXL library, you can efficiently manage CSV data in your C# applications. IronXL offers a free trial for users to experience its features before committing to a purchase. This fully functional trial allows potential users to test and evaluate the product in a live environment without any watermarks in production. After the trial period, if you decide to continue using IronXL for your projects, the licensing for the product starts from $799. 常见问题解答 我如何在C#中从CSV文件读取数据? 您可以使用IronXL库在C#中从CSV文件读取数据。IronXL提供`WorkBook`和`WorkSheet`类,允许您像处理Excel电子表格一样加载和操作CSV文件。 使用C#将CSV数据存储在SQL Server数据库中有哪些步骤? 要使用C#将CSV数据存储在SQL Server数据库中,首先使用IronXL读取CSV,然后将数据加载到`DataTable`中,并使用`SqlBulkCopy`将数据高效地插入到SQL Server表中。 如何在我的C#项目中安装IronXL? 您可以通过Visual Studio中的NuGet包管理器在您的C#项目中安装IronXL。在NuGet包管理器中搜索'IronXL'并将其添加到您的项目中。 使用IronXL处理C#中的CSV文件的主要优点是什么? 使用IronXL处理C#中的CSV文件的主要优点是它能够在.NET应用程序中无缝管理和操作CSV和Excel文件,兼容各种.NET版本和操作系统。 我可以使用IronXL处理Excel文件以及CSV文件吗? 可以,IronXL设计用于处理Excel和CSV文件,在.NET应用程序中是一个多功能的工具,用于管理电子表格数据。 如果我在使用IronXL读取CSV文件时遇到错误该怎么办? 如果您在使用IronXL读取CSV文件时遇到错误,请确保CSV文件格式正确并且IronXL已正确安装在您的项目中。您可以参考IronXL的文档以获得故障排除提示。 我如何在购买前测试IronXL的功能? IronXL提供完全功能的免费试用版,允许您在生产环境中无任何限制地测试和评估产品。 使用IronXL将CSV数据存储在数据库中需要什么先决条件? 先决条件包括安装Visual Studio、访问SQL Server,并通过NuGet安装IronXL。您还需要设置一个SQL Server表来存储CSV数据。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已发布十月 27, 2025 如何在 C# 中创建 Excel 数据透视表 学习通过这个清晰的分步指南使用C# Interop和IronXL在Excel中创建数据透视表。 阅读更多 已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多 已发布十月 27, 2025 如何在.NET Core中使用CSV Reader与IronXL 学习通过实际示例有效地使用IronXL作为.NET Core的CSV读取器。 阅读更多 如何在 C# 中导出 Datagridview 到 Excel如何在 C# 中将 Excel 文件转...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多