#region Constants
private const string transConflictMessage = "Must not specify SqlBulkCopyOptions.UseInternalTransaction " +
"and pass an external Transaction at the same time.";
+
+ private const SqlBulkCopyOptions insertModifiers =
+ SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.TableLock |
+ SqlBulkCopyOptions.KeepNulls | SqlBulkCopyOptions.FireTriggers;
#endregion
#region Fields
private string _destinationTableName = null;
private bool ordinalMapping = false;
private bool sqlRowsCopied = false;
- private bool identityInsert = false;
private bool isLocalConnection = false;
private SqlConnection connection;
private SqlTransaction externalTransaction;
this.connection = new SqlConnection (connectionString);
isLocalConnection = true;
- switch (copyOptions) {
- case SqlBulkCopyOptions.Default:
- this.copyOptions = copyOptions;
- break;
-
- default:
- throw new NotImplementedException ("We don't know how to process non-default copyOptions.");
- }
+ if ((copyOptions & SqlBulkCopyOptions.UseInternalTransaction) == SqlBulkCopyOptions.UseInternalTransaction)
+ throw new NotImplementedException ("We don't know how to process UseInternalTransaction option.");
+
+ this.copyOptions = copyOptions;
}
[MonoTODO]
else
this.externalTransaction = externalTransaction;
- switch (copyOptions) {
- case SqlBulkCopyOptions.Default:
- this.copyOptions = copyOptions;
- break;
-
- default:
- throw new NotImplementedException ("We don't know how to process non-default copyOptions.");
- }
+ if ((copyOptions & SqlBulkCopyOptions.UseInternalTransaction) == SqlBulkCopyOptions.UseInternalTransaction)
+ throw new NotImplementedException ("We don't know how to process UseInternalTransaction option.");
+
+ this.copyOptions = copyOptions;
}
#endregion
throw new InvalidOperationException ("This method should not be called on a closed connection");
if (_destinationTableName == null)
throw new ArgumentNullException ("DestinationTableName");
- if (identityInsert) {
+ if (isLocalConnection && connection.State != ConnectionState.Open)
+ connection.Open();
+
+ if ((copyOptions & SqlBulkCopyOptions.KeepIdentity) == SqlBulkCopyOptions.KeepIdentity) {
SqlCommand cmd = new SqlCommand ("set identity_insert " +
table.TableName + " on",
connection);
string statement = "insert bulk " + DestinationTableName + " (";
statement += GenerateColumnMetaData (tmpCmd, colMetaData, tableCollations);
statement += ")";
+
+ #region Check requested options and add corresponding modifiers to the statement
+ if ((copyOptions & insertModifiers) != SqlBulkCopyOptions.Default) {
+ statement += " WITH (";
+ bool commaRequired = false;
+
+ if ((copyOptions & SqlBulkCopyOptions.CheckConstraints) == SqlBulkCopyOptions.CheckConstraints) {
+ if (commaRequired)
+ statement += ", ";
+ statement += "CHECK_CONSTRAINTS";
+ commaRequired = true;
+ }
+
+ if ((copyOptions & SqlBulkCopyOptions.TableLock) == SqlBulkCopyOptions.TableLock) {
+ if (commaRequired)
+ statement += ", ";
+ statement += "TABLOCK";
+ commaRequired = true;
+ }
+
+ if ((copyOptions & SqlBulkCopyOptions.KeepNulls) == SqlBulkCopyOptions.KeepNulls) {
+ if (commaRequired)
+ statement += ", ";
+ statement += "KEEP_NULLS";
+ commaRequired = true;
+ }
+
+ if ((copyOptions & SqlBulkCopyOptions.FireTriggers) == SqlBulkCopyOptions.FireTriggers) {
+ if (commaRequired)
+ statement += ", ";
+ statement += "FIRE_TRIGGERS";
+ commaRequired = true;
+ }
+
+ statement += ")";
+ }
+ #endregion Check requested options and add corresponding modifiers to the statement
+
blkCopy.SendColumnMetaData (statement);
}
blkCopy.BulkCopyStart (tmpCmd.Parameters.MetaParameters);