The code found on the internet hasn't been verified yet.
/// <summary> ///Bulk inserts multiple rows to SQL/// </summary> /// <param name= "TableName" >The name of the table to insert</param> /// <param name= "Primarykeyname" >The name of the primary key column of the table</param> /// <param name= "AutoIncrement" >True If the primary key is automatically allocated by the DB</param> /// <param name= "Pocos" >The POCO objects that specifies the column values to be inserted</param> /// <param name= "BatchSize" >The number of Pocos to is grouped together for each database Rounddtrip</param> Public voidBulkinsert (stringTableName,stringPrimarykeyname,BOOLAutoIncrement, ienumerable<Object> Pocos,intBatchSize = -) { Try{opensharedconnection (); Try { using(varcmd = CreateCommand (_sharedconnection,"")) { varPD =Pocodata.forobject (Pocos. First (), primarykeyname); //Create List of ColumnNames only once varNames =Newlist<string>(); foreach(varIinchPD. Columns) {//Don ' t insert result columns if(I.value.resultcolumn)Continue; //Don ' t insert the primary KEY (except under Oracle where we need bring in the next sequence value) if(AutoIncrement && Primarykeyname! =NULL&&string. Compare (I.key, Primarykeyname,true) ==0) { //Setup auto Increment expression stringAutoincexpression =_dbtype.getautoincrementexpression (PD. Tableinfo); if(Autoincexpression! =NULL) {names. ADD (I.key); } Continue; } names. ADD (_dbtype.escapesqlidentifier (I.key)); } varNamesarray =names. ToArray (); varValues =Newlist<string>(); intCount =0; Do{Cmd.commandtext=""; Cmd. Parameters.clear (); varindex =0; foreach(varPocoinchPocos. Skip (count). Take (batchsize)) {values. Clear (); foreach(varIinchPD. Columns) {//Don ' t insert result columns if(I.value.resultcolumn)Continue; //Don ' t insert the primary KEY (except under Oracle where we need bring in the next sequence value) if(AutoIncrement && Primarykeyname! =NULL&&string. Compare (I.key, Primarykeyname,true) ==0) { //Setup auto Increment expression stringAutoincexpression =_dbtype.getautoincrementexpression (PD. Tableinfo); if(Autoincexpression! =NULL) {values. ADD (autoincexpression); } Continue; } values. ADD (string. Format ("{0}{1}", _paramprefix, index++)); AddParam (cmd, I.value.getvalue (Poco), i.value.propertyinfo); } stringOutputclause =String.Empty; if(AutoIncrement) {Outputclause=_dbtype.getinsertoutputclause (primarykeyname); } Cmd.commandtext+=string. Format ("INSERT into {0} ({1}) {2} VALUES ({3})", _dbtype.escapetablename (tableName),string. Join (",", Namesarray), Outputclause,string. Join (",", values. ToArray ())); } //Is we done? if(Cmd.commandtext = ="") Break; Count+=batchsize; Dopreexecute (CMD); Cmd. ExecuteNonQuery (); Onexecutedcommand (CMD); } while(true); } } finally{closesharedconnection (); } } Catch(Exception x) {if(Onexception (x))Throw; } } /// <summary> ///performs a SQL Bulk Insert/// </summary> /// <param name= "Pocos" >The POCO objects that specifies the column values to be inserted</param> /// <param name= "BatchSize" >The number of Pocos to is grouped together for each database Rounddtrip</param> Public voidBulkinsert (ienumerable<Object> Pocos,intBatchSize = -) { if(!pocos. Any ())return; varPD =Pocodata.fortype (Pocos. First (). GetType ()); Bulkinsert (PD. Tableinfo.tablename, PD. Tableinfo.primarykey, PD. Tableinfo.autoincrement, Pocos); }
Excerpt from: http://stackoverflow.com/questions/6595105/bulk-insert-update-with-petapoco/14479073
Petapoco BULK INSERT Data