Quantcast
Viewing all articles
Browse latest Browse all 12

Answer by simaglei for How should I multiple insert multiple records?

Following up @Tim Mahy - There's two possible ways to feed SqlBulkCopy: a DataReader or via DataTable. Here the code for DataTable:

DataTable dt = new DataTable();dt.Columns.Add(new DataColumn("Id", typeof(string)));dt.Columns.Add(new DataColumn("Name", typeof(string)));foreach (Entry entry in entries)    dt.Rows.Add(new string[] { entry.Id, entry.Name });using (SqlBulkCopy bc = new SqlBulkCopy(connection)){   // the following 3 lines might not be neccessary    bc.DestinationTableName = "Entries";    bc.ColumnMappings.Add("Id", "Id");    bc.ColumnMappings.Add("Name", "Name");    bc.WriteToServer(dt);}

Viewing all articles
Browse latest Browse all 12

Trending Articles