This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
forked from lukemcgregor/StaticVoid.OrmPerformance
-
Notifications
You must be signed in to change notification settings - Fork 1
/
InsertSqlBulkConfiguration.cs
57 lines (48 loc) · 1.68 KB
/
InsertSqlBulkConfiguration.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StaticVoid.OrmPerformance.Harness.Contract;
namespace StaticVoid.OrmPerformance.Harness.SqlCommand {
public class InsertSqlBulkConfiguration : IRunnableInsertConfiguration {
public string Name { get { return "SqlBulkCopy"; } }
public string Technology { get { return "SqlCommand"; } }
private IConnectionString _connectionString;
private SqlConnection _connection;
private DataTable _dataToAdd;
public InsertSqlBulkConfiguration(IConnectionString connectionString) {
_connectionString = connectionString;
}
public void Setup() {
_connection = new System.Data.SqlClient.SqlConnection(_connectionString.FormattedConnectionString);
_connection.Open();
_dataToAdd = new DataTable();
_dataToAdd.Columns.Add(new DataColumn("TestString", typeof(string)));
_dataToAdd.Columns.Add(new DataColumn("TestDate", typeof(DateTime)));
_dataToAdd.Columns.Add(new DataColumn("TestInt", typeof(int)));
}
public void Add(Models.TestEntity entity) {
var row = _dataToAdd.NewRow();
row["TestInt"] = entity.TestInt;
row["TestDate"] = entity.TestDate;
row["TestString"] = entity.TestString;
_dataToAdd.Rows.Add(row);
}
public void Commit() {
using (var bulkCopy = new SqlBulkCopy(_connection)) {
bulkCopy.ColumnMappings.Add(0, 1);
bulkCopy.ColumnMappings.Add(1, 2);
bulkCopy.ColumnMappings.Add(2, 3);
bulkCopy.DestinationTableName = "TestEntities";
bulkCopy.WriteToServer(_dataToAdd);
}
}
public void TearDown() {
_connection.Close();
_connection.Dispose();
}
}
}