简体   繁体   English

从 Excel / CSV 批量插入 SQL 服务器

[英]Bulk insert to SQL Server from Excel / CSV

I need to bulk insert to SQL Server data from an Excel or CSV file.我需要从 Excel 或 CSV 文件批量插入 SQL 服务器数据。 The data is close to 100k lines.数据接近 100k 行。 I am using C#/.NET.我正在使用 C#/.NET。 In the Microsoft documentation I find this:在 Microsoft 文档中,我发现:

BULK INSERT Sales.Invoices
FROM '\\share\invoices\inv-2016-07-25.csv'
WITH (FORMAT = 'CSV'
  , FIRSTROW=2
  , FIELDQUOTE = '\'
  , FIELDTERMINATOR = ';'
  , ROWTERMINATOR = '0x0a');

BULK INSERT (Transact-SQL) 批量插入 (Transact-SQL)

If I have an Excel file I can convert it to CSV and use this.如果我有 Excel 文件,我可以将其转换为 CSV 并使用它。 This works great and it is very efficient, but my problem is that I do not have access to the file system on the SQL server.这很好用而且效率很高,但我的问题是我无法访问 SQL 服务器上的文件系统。 Has anyone an idea what is the best way to work around that or is there maybe a totally different approach that is better?有谁知道解决这个问题的最佳方法是什么,或者是否有更好的完全不同的方法?

I am seeing this problem like client wants to load data to one of the database table for some reason.我看到这个问题,比如客户端出于某种原因想要将数据加载到其中一个数据库表中。

  • if they need to automate this process or they need to perform this so frequently or needs to do more than one file at a time, I would stream line the process by asking the client to copy the CVS files or Excel files to a server folder when they want to import to database.如果他们需要自动化这个过程,或者他们需要如此频繁地执行此过程或需要一次执行多个文件,我会通过要求客户端将 CVS 文件或 Excel 文件复制到服务器文件夹来完成该过程他们想导入数据库。

After then I would write a folder watching .NET application that would reader CVS or Excel files and import data to database table.之后,我将编写一个文件夹来观察 .NET 应用程序,该应用程序将读取 CVS 或 Excel 文件并将数据导入数据库表。 If it fails to load the data, it should move the file to "error files" Folder with error log.如果加载数据失败,它应该将文件移动到带有错误日志的“错误文件”文件夹。 If it is success, application would move that file to "completed files".如果成功,应用程序会将该文件移动到“已完成文件”。

Advantages of this approach.这种方法的优点。

  • multiple users can use this functionality to upload data to server.多个用户可以使用此功能将数据上传到服务器。
  • User machine doesn't require any kind of installation.用户机器不需要任何类型的安装。
  • Access to the folder can completely managed by AD.对文件夹的访问可以完全由 AD 管理。

Disadvantage坏处

  • This is an old approach to handle this kind of use case.这是处理这种用例的一种旧方法。
  • User needs to manually check the log or server folder to know the status of the files.用户需要手动检查日志或服务器文件夹以了解文件的状态。

Technical References技术参考

You can try out SqlBulkCopy , allowing you to insert your data directly to the database by use of a DataTable .您可以试用SqlBulkCopy ,允许您使用DataTable将数据直接插入数据库。

More information can be found here: SqlBulkCopy.WriteToServerAsync .可以在此处找到更多信息: SqlBulkCopy.WriteToServerAsync

Using SQLBulkCopy seems to be a very good solution as it is explained正如解释的那样,使用 SQLBulkCopy 似乎是一个非常好的解决方案

here 这里

Here we have the suggestion of doing this:在这里,我们有这样做的建议:

var lines = System.IO.File.ReadAllLines(@"d:\data.txt");
if (lines.Count() == 0) return;
var columns = lines[0].Split(',');
var table = new DataTable();
foreach (var c in columns)
    table.Columns.Add(c);

for (int i = 1; i < lines.Count() - 1; i++)
    table.Rows.Add(lines[i].Split(','));

var connection = @"your connection string";
var sqlBulk = new SqlBulkCopy(connection);
sqlBulk.DestinationTableName = "Table1";
sqlBulk.WriteToServer(table);

This is pretty efficient and by using this code I can insert 100.000 records in less than 5 secs which is totally acceptable performance.这是非常有效的,通过使用此代码,我可以在不到 5 秒的时间内插入 100.000 条记录,这是完全可以接受的性能。 Direct SQL Bulk insert on the SQL server itself is of course faster and inserts the data almost instantly but when as in my case that is not possible, we have a decent alternative here.直接 SQL SQL 服务器本身的批量插入当然更快,并且几乎可以立即插入数据,但是在我的情况下这是不可能的,我们在这里有一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM