-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlServerBulkInsertOptionFactory.cs
More file actions
60 lines (52 loc) · 1.96 KB
/
Copy pathSqlServerBulkInsertOptionFactory.cs
File metadata and controls
60 lines (52 loc) · 1.96 KB
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
namespace DBAClientX.PowerShell;
internal static class SqlServerBulkInsertOptionFactory
{
internal static SqlServerBulkInsertOptions Create(
bool tableLock,
bool checkConstraints,
bool fireTriggers,
bool keepIdentity,
bool keepNulls,
bool autoCreateTable,
Dictionary<string, string>? columnMappings = null,
int? notifyAfter = null,
Action<long>? rowsCopied = null)
{
var options = new SqlServerBulkInsertOptions
{
ColumnMappings = columnMappings,
AutoCreateTable = autoCreateTable,
NotifyAfter = notifyAfter,
RowsCopied = rowsCopied
};
SetBulkCopyOptions(options, tableLock, checkConstraints, fireTriggers, keepIdentity, keepNulls);
return options;
}
private static void SetBulkCopyOptions(
SqlServerBulkInsertOptions options,
bool tableLock,
bool checkConstraints,
bool fireTriggers,
bool keepIdentity,
bool keepNulls)
{
var property = typeof(SqlServerBulkInsertOptions).GetProperty("BulkCopyOptions")
?? throw new InvalidOperationException("SqlServerBulkInsertOptions.BulkCopyOptions property was not found.");
var enumType = property.PropertyType;
var value = Convert.ToInt64(Enum.Parse(enumType, "Default"), System.Globalization.CultureInfo.InvariantCulture);
AddFlag("TableLock", tableLock);
AddFlag("CheckConstraints", checkConstraints);
AddFlag("FireTriggers", fireTriggers);
AddFlag("KeepIdentity", keepIdentity);
AddFlag("KeepNulls", keepNulls);
property.SetValue(options, Enum.ToObject(enumType, value));
void AddFlag(string name, bool enabled)
{
if (!enabled)
{
return;
}
value |= Convert.ToInt64(Enum.Parse(enumType, name), System.Globalization.CultureInfo.InvariantCulture);
}
}
}