/// <summary> /// Prepares the bulk copy operation. /// </summary> /// <param name="connection">The connection to use.</param> /// <param name="tableName">The name of the table.</param> /// <param name="reader">The reader to read objects from.</param> /// <param name="configure">A callback method to configure the bulk copy object.</param> /// <param name="options">Options for initializing the bulk copy object.</param> /// <param name="transaction">An optional transaction to participate in.</param> /// <returns>The configured bulk copy object.</returns> private static SqlInsightBulkCopy PrepareBulkCopy(IDbConnection connection, string tableName, IDataReader reader, Action <InsightBulkCopy> configure, InsightBulkCopyOptions options, IDbTransaction transaction) { if (reader == null) { throw new ArgumentNullException("reader"); } SqlBulkCopyOptions sqlOptions = SqlBulkCopyOptions.Default; if (options.HasFlag(InsightBulkCopyOptions.KeepIdentity)) { sqlOptions |= SqlBulkCopyOptions.KeepIdentity; } if (options.HasFlag(InsightBulkCopyOptions.FireTriggers)) { sqlOptions |= SqlBulkCopyOptions.FireTriggers; } if (options.HasFlag(InsightBulkCopyOptions.CheckConstraints)) { sqlOptions |= SqlBulkCopyOptions.CheckConstraints; } if (options.HasFlag(InsightBulkCopyOptions.TableLock)) { sqlOptions |= SqlBulkCopyOptions.TableLock; } if (options.HasFlag(InsightBulkCopyOptions.KeepNulls)) { sqlOptions |= SqlBulkCopyOptions.KeepNulls; } if (options.HasFlag(InsightBulkCopyOptions.UseInternalTransaction)) { sqlOptions |= SqlBulkCopyOptions.UseInternalTransaction; } SqlBulkCopy bulk = null; SqlInsightBulkCopy insightBulk = null; try { bulk = new SqlBulkCopy((SqlConnection)connection, sqlOptions, (SqlTransaction)transaction); bulk.DestinationTableName = tableName; bulk.EnableStreaming = true; // map the columns by name, in case we skipped a readonly column for (int i = 0; i < reader.FieldCount; i++) { string fieldName = reader.GetName(i); bulk.ColumnMappings.Add(fieldName, fieldName); } insightBulk = new SqlInsightBulkCopy(bulk); bulk = null; if (configure != null) { configure(insightBulk); } return(insightBulk); } catch { if (insightBulk != null) { insightBulk.Dispose(); } if (bulk != null) { ((IDisposable)bulk).Dispose(); } throw; } }
/// <summary> /// Bulk copies a set of objects to the server. /// </summary> /// <param name="connection">The connection to use.</param> /// <param name="tableName">The name of the table.</param> /// <param name="reader">The reader to read objects from.</param> /// <param name="configure">A callback method to configure the bulk copy object.</param> /// <param name="options">Options for initializing the bulk copy object.</param> /// <param name="transaction">An optional transaction to participate in.</param> /// <remarks>Number of rows copied if supported, -1 otherwise.</remarks> public override void BulkCopy(IDbConnection connection, string tableName, IDataReader reader, Action<InsightBulkCopy> configure, InsightBulkCopyOptions options, IDbTransaction transaction) { if (reader == null) throw new ArgumentNullException("reader"); SqlBulkCopyOptions sqlOptions = SqlBulkCopyOptions.Default; if (options.HasFlag(InsightBulkCopyOptions.KeepIdentity)) sqlOptions |= SqlBulkCopyOptions.KeepIdentity; if (options.HasFlag(InsightBulkCopyOptions.FireTriggers)) sqlOptions |= SqlBulkCopyOptions.FireTriggers; if (options.HasFlag(InsightBulkCopyOptions.CheckConstraints)) sqlOptions |= SqlBulkCopyOptions.CheckConstraints; if (options.HasFlag(InsightBulkCopyOptions.TableLock)) sqlOptions |= SqlBulkCopyOptions.TableLock; if (options.HasFlag(InsightBulkCopyOptions.KeepNulls)) sqlOptions |= SqlBulkCopyOptions.KeepNulls; if (options.HasFlag(InsightBulkCopyOptions.UseInternalTransaction)) sqlOptions |= SqlBulkCopyOptions.UseInternalTransaction; using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)connection, sqlOptions, (SqlTransaction)transaction)) using (var insightBulk = new SqlInsightBulkCopy(bulk)) { bulk.DestinationTableName = tableName; #if !NODBASYNC bulk.EnableStreaming = true; #endif // map the columns by name, in case we skipped a readonly column foreach (DataRow row in reader.GetSchemaTable().Rows) bulk.ColumnMappings.Add((string)row["ColumnName"], (string)row["ColumnName"]); if (configure != null) configure(insightBulk); bulk.WriteToServer(reader); } }
/// <summary> /// Bulk copies a set of objects to the server. /// </summary> /// <param name="connection">The connection to use.</param> /// <param name="tableName">The name of the table.</param> /// <param name="reader">The reader to read objects from.</param> /// <param name="configure">A callback method to configure the bulk copy object.</param> /// <param name="options">Options for initializing the bulk copy object.</param> /// <param name="transaction">An optional transaction to participate in.</param> /// <remarks>Number of rows copied if supported, -1 otherwise.</remarks> public override void BulkCopy(IDbConnection connection, string tableName, IDataReader reader, Action<InsightBulkCopy> configure, InsightBulkCopyOptions options, IDbTransaction transaction) { SqlBulkCopyOptions sqlOptions = SqlBulkCopyOptions.Default; if (options.HasFlag(InsightBulkCopyOptions.KeepIdentity)) sqlOptions |= SqlBulkCopyOptions.KeepIdentity; if (options.HasFlag(InsightBulkCopyOptions.FireTriggers)) sqlOptions |= SqlBulkCopyOptions.FireTriggers; if (options.HasFlag(InsightBulkCopyOptions.CheckConstraints)) sqlOptions |= SqlBulkCopyOptions.CheckConstraints; if (options.HasFlag(InsightBulkCopyOptions.TableLock)) sqlOptions |= SqlBulkCopyOptions.TableLock; if (options.HasFlag(InsightBulkCopyOptions.KeepNulls)) sqlOptions |= SqlBulkCopyOptions.KeepNulls; if (options.HasFlag(InsightBulkCopyOptions.UseInternalTransaction)) sqlOptions |= SqlBulkCopyOptions.UseInternalTransaction; using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)connection, sqlOptions, (SqlTransaction)transaction)) using (var insightBulk = new SqlInsightBulkCopy(bulk)) { bulk.DestinationTableName = tableName; #if !NODBASYNC bulk.EnableStreaming = true; #endif if (configure != null) configure(insightBulk); bulk.WriteToServer(reader); } }
/// <summary> /// Prepares the bulk copy operation. /// </summary> /// <param name="connection">The connection to use.</param> /// <param name="tableName">The name of the table.</param> /// <param name="reader">The reader to read objects from.</param> /// <param name="configure">A callback method to configure the bulk copy object.</param> /// <param name="options">Options for initializing the bulk copy object.</param> /// <param name="transaction">An optional transaction to participate in.</param> /// <returns>The configured bulk copy object.</returns> private SqlInsightBulkCopy PrepareBulkCopy(IDbConnection connection, string tableName, IDataReader reader, Action <InsightBulkCopy> configure, InsightBulkCopyOptions options, IDbTransaction transaction) { if (reader == null) { throw new ArgumentNullException("reader"); } SqlBulkCopyOptions sqlOptions = SqlBulkCopyOptions.Default; if (options.HasFlag(InsightBulkCopyOptions.KeepIdentity)) { sqlOptions |= SqlBulkCopyOptions.KeepIdentity; } if (options.HasFlag(InsightBulkCopyOptions.FireTriggers)) { sqlOptions |= SqlBulkCopyOptions.FireTriggers; } if (options.HasFlag(InsightBulkCopyOptions.CheckConstraints)) { sqlOptions |= SqlBulkCopyOptions.CheckConstraints; } if (options.HasFlag(InsightBulkCopyOptions.TableLock)) { sqlOptions |= SqlBulkCopyOptions.TableLock; } if (options.HasFlag(InsightBulkCopyOptions.KeepNulls)) { sqlOptions |= SqlBulkCopyOptions.KeepNulls; } if (options.HasFlag(InsightBulkCopyOptions.UseInternalTransaction)) { sqlOptions |= SqlBulkCopyOptions.UseInternalTransaction; } SqlBulkCopy bulk = null; SqlInsightBulkCopy insightBulk = null; try { bulk = new SqlBulkCopy((SqlConnection)connection, sqlOptions, (SqlTransaction)transaction); insightBulk = new SqlInsightBulkCopy(bulk); bulk.DestinationTableName = tableName; #if !NODBASYNC bulk.EnableStreaming = true; #endif // map the columns by name, in case we skipped a readonly column foreach (DataRow row in reader.GetSchemaTable().Rows) { bulk.ColumnMappings.Add((string)row["ColumnName"], (string)row["ColumnName"]); } if (configure != null) { configure(insightBulk); } return(insightBulk); } catch { if (insightBulk != null) { insightBulk.Dispose(); } throw; } }
/// <summary> /// Prepares the bulk copy operation. /// </summary> /// <param name="connection">The connection to use.</param> /// <param name="tableName">The name of the table.</param> /// <param name="reader">The reader to read objects from.</param> /// <param name="configure">A callback method to configure the bulk copy object.</param> /// <param name="options">Options for initializing the bulk copy object.</param> /// <param name="transaction">An optional transaction to participate in.</param> /// <returns>The configured bulk copy object.</returns> private static SqlInsightBulkCopy PrepareBulkCopy(IDbConnection connection, string tableName, IDataReader reader, Action<InsightBulkCopy> configure, InsightBulkCopyOptions options, IDbTransaction transaction) { if (reader == null) throw new ArgumentNullException("reader"); SqlBulkCopyOptions sqlOptions = SqlBulkCopyOptions.Default; if (options.HasFlag(InsightBulkCopyOptions.KeepIdentity)) sqlOptions |= SqlBulkCopyOptions.KeepIdentity; if (options.HasFlag(InsightBulkCopyOptions.FireTriggers)) sqlOptions |= SqlBulkCopyOptions.FireTriggers; if (options.HasFlag(InsightBulkCopyOptions.CheckConstraints)) sqlOptions |= SqlBulkCopyOptions.CheckConstraints; if (options.HasFlag(InsightBulkCopyOptions.TableLock)) sqlOptions |= SqlBulkCopyOptions.TableLock; if (options.HasFlag(InsightBulkCopyOptions.KeepNulls)) sqlOptions |= SqlBulkCopyOptions.KeepNulls; if (options.HasFlag(InsightBulkCopyOptions.UseInternalTransaction)) sqlOptions |= SqlBulkCopyOptions.UseInternalTransaction; SqlBulkCopy bulk = null; SqlInsightBulkCopy insightBulk = null; try { bulk = new SqlBulkCopy((SqlConnection)connection, sqlOptions, (SqlTransaction)transaction); bulk.DestinationTableName = tableName; #if !NODBASYNC bulk.EnableStreaming = true; #endif // map the columns by name, in case we skipped a readonly column foreach (DataRow row in reader.GetSchemaTable().Rows) bulk.ColumnMappings.Add((string)row["ColumnName"], (string)row["ColumnName"]); insightBulk = new SqlInsightBulkCopy(bulk); bulk = null; if (configure != null) configure(insightBulk); return insightBulk; } catch { if (insightBulk != null) insightBulk.Dispose(); if (bulk != null) ((IDisposable)bulk).Dispose(); throw; } }