У меня есть List<MyObject>
с миллионом элементов. (На самом деле это SubSonic Collection, но она не загружается из базы данных).
В настоящее время я использую SqlBulkCopy следующим образом:
private string FastInsertCollection(string tableName, DataTable tableData)
{
string sqlConn = ConfigurationManager.ConnectionStrings[SubSonicConfig.DefaultDataProvider.ConnectionStringName].ConnectionString;
using (SqlBulkCopy s = new SqlBulkCopy(sqlConn, SqlBulkCopyOptions.TableLock))
{
s.DestinationTableName = tableName;
s.BatchSize = 5000;
s.WriteToServer(tableData);
s.BulkCopyTimeout = SprocTimeout;
s.Close();
}
return sqlConn;
}
Я использую SubSonic MyObjectCollection.ToDataTable() для создания DataTable из моей коллекции. Однако это дублирует объекты в памяти и неэффективно. Я хотел бы использовать метод SqlBulkCopy.WriteToServer, который использует IDataReader вместо DataTable, чтобы я не дублировал свою коллекцию в памяти.
Какой самый простой способ получить IDataReader из моего списка? Я полагаю, что я мог бы реализовать пользовательский считыватель данных (например, http://blogs.microsoft.co.il/blogs/aviwortzel/archive/2008/05/06/implementing-sqlbulkcopy-in-linq-to-sql.aspx), но должно быть что-то более простое, что я могу сделать, не написав кучу общий код.
Изменить: Не кажется, что можно легко создать IDataReader из коллекции объектов. Принимая текущий ответ, хотя я надеялся на что-то встроенное в рамки.