C# .NET - Sql Bulk Insert from multiple delimited Textfile using c#.net

SqlBulkCopy.WriteToServer has 4 overloads:
SqlBulkCopy.WriteToServer (DataRow[])
    Copies all rows from the supplied DataRow array to a destination table specified by the 
    DestinationTableName property of the SqlBulkCopy object. 
SqlBulkCopy.WriteToServer (DataTable)
    Copies all rows in the supplied DataTable to a destination table specified by the 
    DestinationTableName property of the SqlBulkCopy object. 
SqlBulkCopy.WriteToServer (IDataReader)
    Copies all rows in the supplied IDataReader to a destination table specified by the 
    DestinationTableName property of the SqlBulkCopy object. 
SqlBulkCopy.WriteToServer (DataTable, DataRowState)
    Copies only rows that match the supplied row state in the supplied DataTable to a 
    destination table specified by the DestinationTableName property of the SqlBulkCopy object.

When importing text files with this method you have to create a DataTable first, import the text file 
to the created DataTable and then write this DataTable to server.

With this we‘re acctually performing 2 tasks in .net:
1. Fill data from text file to DataTable in memory
2. Fill data from DataTable in memory to SQL server

Compared to SQL servers native bulk import methods where we just import the text file directly.

I used the same file and the same table structure as in previous bulk import methods described in Last
The time it took to complete the whole process was around 30 seconds.

This is the code i used for import:

private void StartImport()
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
    SqlBulkCopy bulkCopy = new SqlBulkCopy("Server=ServerName;Database=test;Trusted_Connection=True;",
        SqlBulkCopyOptions.TableLock);
    bulkCopy.DestinationTableName = "dbo.testSQLBulkCopy";
    bulkCopy.WriteToServer(CreateDataTableFromFile());
    sw.Stop();
    txtResult.Text = (sw.ElapsedMilliseconds/1000.00).ToString();
}
private DataTable CreateDataTableFromFile()
{
    DataTable dt = new DataTable();
    DataColumn dc;
    DataRow dr;

    dc = new DataColumn();
    dc.DataType = System.Type.GetType("System.Int32");
    dc.ColumnName = "c1";
    dc.Unique = false;
    dt.Columns.Add(dc);
    dc = new DataColumn();
    dc.DataType = System.Type.GetType("System.Int32");
    dc.ColumnName = "c2";
    dc.Unique = false;
    dt.Columns.Add(dc);
    dc = new DataColumn();
    dc.DataType = System.Type.GetType("System.Int32");
    dc.ColumnName = "c3";
    dc.Unique = false;
    dt.Columns.Add(dc);
    dc = new DataColumn();
    dc.DataType = System.Type.GetType("System.Int32");
    dc.ColumnName = "c4";
    dc.Unique = false;
    dt.Columns.Add(dc);
    StreamReader sr = new StreamReader(@"d:\work\test.txt");
    string input;
    while ((input = sr.ReadLine()) != null)
    {
        string[] s = input.Split(new char[] { ‘|‘ });
        dr = dt.NewRow();
        dr["c1"] = s[0];
        dr["c2"] = s[1];
        dr["c3"] = s[2];
        dr["c4"] = s[3];
        dt.Rows.Add(dr);
    }
    sr.Close();
    return dt;
}

Bulk Import Methods are ad below..:-

1. BCP
2. Bulk Insert
3. OpenRowset with BULK option
4. SQL Server Integration Services - SSIS

I ran each bulk import option 12 times, disregarded best and worst time and averaged the remaining ten times.
Results are:

1. SSIS - FastParse ON = 7322 ms
2. SSIS - FastParse OFF = 8387 ms
3. Bulk Insert = 10534 ms
4. OpenRowset = 10687 ms
5. BCP = 14922 ms

So speed gain is quite large when using FastParse.
I was also surprised that SSIS - FastParse OFF method was faster by 20% to Bulk Insert and OpenRowset
and around 40% faster than BCP.

Since my desire was to test how much faster is importing flat files when FastParse option is used
I created a text file containing 4 bigint columns with 1,000,000 rows.

The script i used to create a sample test file in C#:

string str;
StreamWriter sw = new StreamWriter(@"d:\work\test.txt");
for (int i = 1; i <= 1000000; i++)
{
    str = i.ToString() + "|" + Convert.ToString(i * 2) + "|" + Convert.ToString(i * 3) + "|" + Convert.ToString(i / 2);
    sw.WriteLine(str);
}
sw.Close();

I also created this format file for use with BCP, Bulk Insert and OpenRowset:

9.0
4
1       SQLBIGINT        0       8       "|"   1     c1       ""
2       SQLBIGINT        0       8       "|"   2     c2       ""
3       SQLBIGINT        0       8       "|"   3     c3       ""
4       SQLBIGINT        0       8       "\r\n"   4     c4       ""

SSIS Package was a very simple one with a Flat File source and SQL server destination objects.

The sql script i used is:

create database test
go
use test
go
-- ran for each SSIS test run
-- SSIS data type for each column was "eight-byte signed integer [DT_I8]"
drop table testFastParse
create table testFastParse(c1 bigint, c2 bigint, c3 bigint, c4 bigint)
go
-- insert data using OPENROWSET
create table testOpenRowset(c1 bigint, c2 bigint, c3 bigint, c4 bigint)
go
DBCC DROPCLEANBUFFERS
declare @start datetime
set @start = getdate()
insert into testOpenRowset(c1, c2, c3, c4)
SELECT    t1.c1, t1.c2, t1.c3, t1.c4
FROM    OPENROWSET( BULK ‘d:\work\test.txt‘,
        FORMATFILE = ‘d:\work\testImport-f-n.Fmt‘) AS t1(c1, c2, c3, c4);
select  getdate() - @start as ElapsedTime
drop table testOpenRowset
-- insert data using Bulk Insert
create table testBulkInsert(c1 bigint, c2 bigint, c3 bigint, c4 bigint)
go
DBCC DROPCLEANBUFFERS
declare @start datetime
set @start = getdate()
BULK INSERT testBulkInsert
   FROM ‘d:\work\test.txt‘
   WITH (FORMATFILE=‘d:\work\testImport-f-n.Fmt‘)
select  getdate() - @start as ElapsedTime
drop table testBulkInsert
go
-- insert data using BCP
create table testBCP(c1 bigint, c2 bigint, c3 bigint, c4 bigint)
go
DBCC DROPCLEANBUFFERS
exec master..xp_cmdshell ‘bcp test.dbo.testBCP in d:\work\test.txt -T -b1000000 -fd:\work\testImport-f-n.Fmt‘
drop table testBCP
go
drop database test
时间: 2024-10-20 13:42:03

C# .NET - Sql Bulk Insert from multiple delimited Textfile using c#.net的相关文章

sql bulk insert

create table test (id int IDENTITY, amount int check(amount >=1000 and amount<=5000)); SELECT * FROM dbo.test /* 下面这个语句不检查约束: */ SET IDENTITY_INSERT dbo.test OFF bulk insert dbo.test from 'f:\test.txt' with (fieldterminator=',', rowterminator='\n')

SQL Bulk Insert 快速插入

SQL INSERT INTO 语句 INSERT INTO 语句用于向表中插入新记录. SQL INSERT INTO 语法 INSERT INTO 语句可以有两种编写形式. ① 第一种形式无需指定要插入数据的列名,只需提供被插入的值即可: INSERT INTO table_nameVALUES (value1,value2,value3,...); ② 第二种形式需要指定列名及被插入的值: INSERT INTO table_name (column1,column2,column3,..

SQL SERVER 使用BULK Insert将txt文件中的数据批量插入表中(1)

1/首先建立数据表 CREATE TABLE BasicMsg( RecvTime FLOAT NOT NULL , --接收时间,不存在时间相同的数据 AA INT NOT NULL, --24位地址码 . FlightID Varchar(10) NULL, --航班号) 2/ 建立存储过程 USE DF17DataProIF EXISTS (SELECT * FROM SYS.PROCEDURES WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[BulkDataP

BULK INSERT如何将大量数据高效地导入SQL Server

转载自:http://database.51cto.com/art/201108/282631.htm BULK INSERT如何将大量数据高效地导入SQL Server 本文我们详细介绍了BULK INSERT将大量数据高效地导入SQL Server数据库的方法,希望本次的介绍能够对您有所帮助. AD:WOT2014课程推荐:实战MSA:用开源软件搭建微服务系统 在实际的工作需要中,我们有时候需将大量的数据导入到数据库中.这时候我们不得不考虑的就是效率问题.本文我们就介绍了一种将大量数据高效地

SQL Server批量数据导出导入Bulk Insert使用

简介 Bulk insert命令区别于BCP命令之处在于它是SQL server脚本语句,它可以将本地或远程的文件数据批量导入数据库,速度非常之快:远程文件必须共享才行, 文件路径须使用通用约定(UNC)名称,即"\\服务器名或IP\共享名\路径\文件名"的形式. 注意,这里的远程事相对数据库服务器而言,即数据文件若放置在数据库服务器之外,则需要共享给数据库服务器: Bulk Insert通常配合BCP导出的格式化文件批量导入数据 Bulk Insert配合格式文件语法 Bulk in

Sql server bulk insert文本文件插入到数据库

Bulk Insert Sql server 的bulk insert语句可以高效的导入大数据量的平面文件(txt,csv文件)到数据库的一张表中,其用法如下: bulk insert test from 'f:\test.txt' with (fieldterminator=',', rowterminator='\n') 其中"test"是数据库表的名字,"f:\test.txt"是导入平面文件的地址,fieldterminator指定平面文件中列的分隔符是什么

Bulk Insert Syntax

BULK INSERT Imports a data file into a database table or view in a user-specified format. BULK INSERT [ database_name . [ schema_name ] . | schema_name . ] [ table_name | view_name ] FROM 'data_file' [ WITH ( [ [ , ] BATCHSIZE = batch_size ] [ [ , ]

Bulk Insert:将文本数据(csv和txt)导入到数据库中

将文本数据导入到数据库中的方法有很多,将文本格式(csv和txt)导入到SQL Server中,bulk insert是最简单的实现方法 1,bulk insert命令,经过简化如下 BULK INSERT schema_name . table_name FROM 'data_file' WITH ( FIELDTERMINATOR = 'field_terminator', ROWTERMINATOR = 'row_terminator', DATAFILETYPE=‘WideChar’ )

Bulk Insert命令具体

BULK INSERT以用户指定的格式复制一个数据文件至数据库表或视图中. 语法: Sql代码 BULK INSERT [ [ 'database_name'.][ 'owner' ].]{ 'table_name' FROM 'data_file' } WITH  ( [ BATCHSIZE [ = batch_size ] ], [ CHECK_CONSTRAINTS ], [ CODEPAGE [ = 'ACP' | 'OEM' | 'RAW' | 'code_page' ] ], [ D