Converting List To IDataReader

I wrote about using SqlBulkCopy to fast load data from .NET into SQL Server database. This class needs a DataTable or IDataReader instance as a source. You can convert a C# List to DataTable (look here Converting List To DataTable). Now I’ll show a couple of examples how to convert List to IDataReader.

1. Straight implementation of IDataReader interface
I’ve seen a couple of good examples from Bruce Dunwiddie https://www.csvreader.com/posts/GenericListDataReader.cs and Venu Gopal http://technico.qnownow.com/custom-data-reader-to-bulk-copy-data-from-object-collection-to-sql-server/. Based on these two solutions I’ve made my one.

Now you can iterate through IDataReader like you do with SqlDataReader.

2. FastMember NuGet package

Thank you, Marc! it’s a good job https://github.com/mgravell/fast-member

You need to install NuGet package FastMember, add using FastMember; statement, and run this code.

CategoriesC#

Leave a Reply

Your email address will not be published. Required fields are marked *