-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateSqlInserter.cs
93 lines (77 loc) · 3.14 KB
/
GenerateSqlInserter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BulkInsertInvestigation
{
public class GenerateSqlInserter : IInserter
{
private string connectionString;
private int batchSize;
public GenerateSqlInserter(string connectionString, int batchSize)
{
this.connectionString = connectionString;
this.batchSize = batchSize;
}
public void Insert(IEnumerable<Customer> dataRecords)
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var transaction = connection.BeginTransaction();
IEnumerable<Customer> batch = null;
int inserted = 0;
do
{
batch = dataRecords.Skip(inserted).Take(this.batchSize);
if (!batch.Any())
break;
var command = connection.CreateCommand();
command.CommandText = GenerateBatchInsertSql(batch);
command.CommandType = CommandType.Text;
command.Transaction = transaction;
//command.ExecuteNonQuery();
var dataReader = command.ExecuteReader();
dataReader.Close();
inserted += batchSize;
Console.WriteLine("Inserted {0} rows", inserted);
} while (batch.Any());
transaction.Commit();
}
}
private string GenerateBatchInsertSql(IEnumerable<Customer> customers)
{
var stringBuilder = new StringBuilder();
stringBuilder.Append(@"
DECLARE @InsertResults TABLE(
Email NVARCHAR(256),
Inserted BIT NOT NULL,
Error NVARCHAR(MAX) NULL
);");
foreach (Customer customer in customers)
{
stringBuilder.AppendFormat(@"
INSERT INTO Customers (Email, Name, [Address], IsActive, Latitude, Longitude)
OUTPUT '{0}', 1, NULL INTO @InsertResults
VALUES ('{0}', '{1}', '{2}', {3}, {4}, {5}) "
/*@"
BEGIN TRY
INSERT INTO Customers (Email, Name, [Address], IsActive, Latitude, Longitude)
OUTPUT '{0}', 1, NULL INTO @InsertResults
VALUES('{0}', '{1}', '{2}', {3}, {4}, {5})
END TRY
BEGIN CATCH
INSERT INTO @InsertResults
VALUES ('{0}', 0, ERROR_MESSAGE())
END CATCH;
"*/,customer.Email, customer.Name, customer.Address, customer.IsActive ? 1 : 0,
customer.Latitude, customer.Longitude);
}
stringBuilder.Append("SELECT * FROM @InsertResults");
return stringBuilder.ToString();
}
}
}