
In .NET, there is a very fast and easy way to pump a variety of entities into an SQL database: SqlBulkCopy
1public void BulkInsert<TEntity>(string connectionString, string tableName, IList<TEntity> entities, params string[] columns)
2{
3 using SqlBulkCopy sqlCopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.KeepNulls)
4 using ObjectReader reader = ObjectReader.Create(entities, columns);
5 {
6 sqlCopy.DestinationTableName = tableName;
7 sqlCopy.WriteToServer(reader);
8 }
9}
Usage:
1public class MyEntity
2{
3 public Id {get;set}
4 public Name {get;set;}
5 public EMail {get;set;}
6}
7
8public class MySqlExporter
9{
10 public void Export()
11 {
12 string connectionString = "your sql connection string";
13 string sqlTableName = "MyEntitiesTable";
14 List<MyEntity> myEntities = ... // here your code to read your existing entities or source
15
16 string[] copyParameters = {
17 nameof(MyEntity.Id)
18 nameof(MyEntity.Name),
19 nameof(MyEntity.EMail),
20 }
21
22 BulkInsert(connectionString, sqlTableName, myEntities, copyParameters);
23 }
24}
Related articles

Apr 10, 2019 - 1 min read
Load an image into a byte-array with C#
This pretty simply snippet exports your Image into a byte-array. 1public static byte[] ToByteArray(this System.Drawing.Image image) 2{ 3 …

Apr 04, 2019 - 1 min read
Download a file with .NET
The most current and currently recommended way to download .NET Framework, .NET Standard or .NET Core files from the Internet is the …

Sep 09, 2014 - 3 min read
Einfaches WCF-Beispiel ohne komplizierte Config
Unter anderem durch die Aktivität im myCSharp.de-Forum ist mir aufgefallen, dass viele Entwickler doch Probleme mit der Konfiguration von …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.
