@@ -1094,6 +1094,68 @@ public void BulkCopyNullDataReader()
1094
1094
var bulkCopy = new MySqlBulkCopy ( connection ) ;
1095
1095
Assert . Throws < ArgumentNullException > ( ( ) => bulkCopy . WriteToServer ( default ( DbDataReader ) ) ) ;
1096
1096
}
1097
+
1098
+ [ Theory ]
1099
+ [ InlineData ( MySqlBulkLoaderConflictOption . None , 1 , "one" ) ]
1100
+ [ InlineData ( MySqlBulkLoaderConflictOption . Ignore , 1 , "one" ) ]
1101
+ [ InlineData ( MySqlBulkLoaderConflictOption . Replace , 3 , "two" ) ]
1102
+ public void BulkCopyDataTableConflictOption ( MySqlBulkLoaderConflictOption conflictOption , int expectedRowsInserted , string expected )
1103
+ {
1104
+ var dataTable = new DataTable ( )
1105
+ {
1106
+ Columns =
1107
+ {
1108
+ new DataColumn ( "id" , typeof ( int ) ) ,
1109
+ new DataColumn ( "data" , typeof ( string ) ) ,
1110
+ } ,
1111
+ Rows =
1112
+ {
1113
+ new object [ ] { 1 , "one" } ,
1114
+ new object [ ] { 1 , "two" } ,
1115
+ } ,
1116
+ } ;
1117
+
1118
+ using var connection = new MySqlConnection ( GetLocalConnectionString ( ) ) ;
1119
+ connection . Open ( ) ;
1120
+ using ( var cmd = new MySqlCommand ( @"drop table if exists bulk_load_data_table;
1121
+ create table bulk_load_data_table(a int not null primary key auto_increment, b text);" , connection ) )
1122
+ {
1123
+ cmd . ExecuteNonQuery ( ) ;
1124
+ }
1125
+
1126
+ var bulkCopy = new MySqlBulkCopy ( connection )
1127
+ {
1128
+ ConflictOption = conflictOption ,
1129
+ DestinationTableName = "bulk_load_data_table" ,
1130
+ } ;
1131
+
1132
+ switch ( conflictOption )
1133
+ {
1134
+ case MySqlBulkLoaderConflictOption . None :
1135
+ var exception = Assert . Throws < MySqlException > ( ( ) => bulkCopy . WriteToServer ( dataTable ) ) ;
1136
+ Assert . Equal ( MySqlErrorCode . BulkCopyFailed , exception . ErrorCode ) ;
1137
+ break ;
1138
+
1139
+ case MySqlBulkLoaderConflictOption . Replace :
1140
+ var replaceResult = bulkCopy . WriteToServer ( dataTable ) ;
1141
+ Assert . Equal ( expectedRowsInserted , replaceResult . RowsInserted ) ;
1142
+ Assert . Empty ( replaceResult . Warnings ) ;
1143
+ break ;
1144
+
1145
+ case MySqlBulkLoaderConflictOption . Ignore :
1146
+ var ignoreResult = bulkCopy . WriteToServer ( dataTable ) ;
1147
+ Assert . Equal ( expectedRowsInserted , ignoreResult . RowsInserted ) ;
1148
+ if ( ! connection . ServerVersion . StartsWith ( "5.6." , StringComparison . Ordinal ) )
1149
+ {
1150
+ var error = Assert . Single ( ignoreResult . Warnings ) ;
1151
+ Assert . Equal ( MySqlErrorCode . DuplicateKeyEntry , error . ErrorCode ) ;
1152
+ }
1153
+ break ;
1154
+ }
1155
+
1156
+ using ( var cmd = new MySqlCommand ( "select b from bulk_load_data_table;" , connection ) )
1157
+ Assert . Equal ( expected , cmd . ExecuteScalar ( ) ) ;
1158
+ }
1097
1159
#endif
1098
1160
1099
1161
internal static string GetConnectionString ( ) => AppConfig . ConnectionString ;
0 commit comments