I have code as follows:
IEnumerable<A> data = GetData();
DataTable dataTable = ConvertDataToDataTable(data);
...//initialize SqlBulkCopy
sqlBulkCopy.WriteToServer(dataTable);
But I do not wish to build up the entire data source in memory in the DataTable. I think this would be better:
IEnumerable<A> data = GetData();
IDataReader dataReader = ConvertDataToDataReader(data);
...//initialize SqlBulkCopy
sqlBulkCopy.WriteToServer(dataReader);
The assumption is that the data would only build up in memory up to the sqlBulkCopy.BatchSize, and then be discarded once written to SQL Server (and also that it would run faster since it can start writing to SQL Server as soon as the first batch of data is available).
However, the IDataReader interface is huge and I am reluctant to implement it myself.
Is there any other solution to this, or something out there that might help me like a ToDataReader() extension method on an IEnumerable?
Edit: Ok this is a duplicated question of Get an IDataReader from a typed List. Looks like one has to implement IDataReader from scratch, there is nothing built into the framework or into a regularly updated NuGet package.