Is there a way to use SqlBulkCopy without converting the data to a DataTable? I have a list of objects (List) in RAM and I really don't want to use more memory to create the DataTable. Could it be possible to implement IDataReader on a List?
Thanks!
By default, a bulk copy operation is its own transaction. When you want to perform a dedicated bulk copy operation, create a new instance of SqlBulkCopy with a connection string, or use an existing SqlConnection object without an active transaction.
The SqlBulkCopy class can be used to write data only to SQL Server tables. However, the data source is not limited to SQL Server; any data source can be used, as long as the data can be loaded to a DataTable instance or read with a IDataReader instance.
I would certainly imagine that you could. BulkDataReader
requires schema information; that's why you can't simply provide a List
. If you design a class that implements IDataReader
, you'll be providing this in your GetSchemaTable
implementation.
I would simply create a DataTable
myself, unless I could demonstrate a real memory issue that would justify the implementation.
As Michael says, you can certainly implement an IDataReader which is the most efficient way of doing it but there is some extra work required. Implementing GetSchemaTable is kind of a pain to implement but it's not that bad if you use the code below as a starting point.
var table = new DataTable( "SchemaTable" );
table.Locale = CultureInfo.InvariantCulture;
table.Columns.Add( new DataColumn( SchemaTableColumn.ColumnName, typeof( string ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.ColumnOrdinal, typeof( int ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.ColumnSize, typeof( int ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.NumericPrecision, typeof( short ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.NumericScale, typeof( short ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.DataType, typeof( Type ) ) );
table.Columns.Add( new DataColumn( SchemaTableOptionalColumn.ProviderSpecificDataType, typeof( Type ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.NonVersionedProviderType, typeof( int ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.ProviderType, typeof( int ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.IsLong, typeof( bool ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.AllowDBNull, typeof( bool ) ) );
table.Columns.Add( new DataColumn( SchemaTableOptionalColumn.IsReadOnly, typeof( bool ) ) );
table.Columns.Add( new DataColumn( SchemaTableOptionalColumn.IsRowVersion, typeof( bool ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.IsUnique, typeof( bool ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.IsKey, typeof( bool ) ) );
table.Columns.Add( new DataColumn( SchemaTableOptionalColumn.IsAutoIncrement, typeof( bool ) ) );
table.Columns.Add( new DataColumn( SchemaTableOptionalColumn.IsHidden, typeof( bool ) ) );
table.Columns.Add( new DataColumn( SchemaTableOptionalColumn.BaseCatalogName, typeof( string ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.BaseSchemaName, typeof( string ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.BaseTableName, typeof( string ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.BaseColumnName, typeof( string ) ) );
table.Columns.Add( new DataColumn( SchemaTableOptionalColumn.BaseServerName, typeof( string ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.IsAliased, typeof( bool ) ) );
table.Columns.Add( new DataColumn( SchemaTableColumn.IsExpression, typeof( bool ) ) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With