1- using Microsoft . Data . SqlClient ;
2- using System . Collections . Generic ;
1+ using System . Collections . Generic ;
32using System . Data ;
43using System . Text ;
5- using System . Threading ;
6- using System . Threading . Tasks ;
74
85namespace EntityFrameworkCore . SqlServer . SimpleBulks . Extensions ;
96
107public static class DataTableExtensions
118{
12- private static readonly BulkOptions DefaultBulkOptions = new BulkOptions ( )
13- {
14- BatchSize = 0 ,
15- Timeout = 30 ,
16- } ;
17-
189 public static string GenerateTableDefinition ( this DataTable table , string tableName ,
1910 IReadOnlyDictionary < string , string > columnNameMappings ,
2011 IReadOnlyDictionary < string , string > columnTypeMappings )
@@ -36,44 +27,6 @@ public static string GenerateTableDefinition(this DataTable table, string tableN
3627 return sql . ToString ( ) ;
3728 }
3829
39- public static void SqlBulkCopy ( this ConnectionContext connectionContext , DataTable dataTable , string tableName , IReadOnlyDictionary < string , string > columnNameMappings , BulkOptions options = null )
40- {
41- options ??= DefaultBulkOptions ;
42-
43- using var bulkCopy = new SqlBulkCopy ( connectionContext . Connection , SqlBulkCopyOptions . Default , connectionContext . Transaction )
44- {
45- BatchSize = options . BatchSize ,
46- BulkCopyTimeout = options . Timeout ,
47- DestinationTableName = $ "{ tableName } "
48- } ;
49-
50- foreach ( DataColumn dtColum in dataTable . Columns )
51- {
52- bulkCopy . ColumnMappings . Add ( dtColum . ColumnName , GetDbColumnName ( dtColum . ColumnName , columnNameMappings ) ) ;
53- }
54-
55- bulkCopy . WriteToServer ( dataTable ) ;
56- }
57-
58- public static async Task SqlBulkCopyAsync ( this ConnectionContext connectionContext , DataTable dataTable , string tableName , IReadOnlyDictionary < string , string > columnNameMappings , BulkOptions options = null , CancellationToken cancellationToken = default )
59- {
60- options ??= DefaultBulkOptions ;
61-
62- using var bulkCopy = new SqlBulkCopy ( connectionContext . Connection , SqlBulkCopyOptions . Default , connectionContext . Transaction )
63- {
64- BatchSize = options . BatchSize ,
65- BulkCopyTimeout = options . Timeout ,
66- DestinationTableName = $ "{ tableName } "
67- } ;
68-
69- foreach ( DataColumn dtColum in dataTable . Columns )
70- {
71- bulkCopy . ColumnMappings . Add ( dtColum . ColumnName , GetDbColumnName ( dtColum . ColumnName , columnNameMappings ) ) ;
72- }
73-
74- await bulkCopy . WriteToServerAsync ( dataTable , cancellationToken ) ;
75- }
76-
7730 private static string GetDbColumnName ( string columnName , IReadOnlyDictionary < string , string > columnNameMappings )
7831 {
7932 if ( columnNameMappings == null )
0 commit comments