Petapoco BULK INSERT Data

Source: Internet
Author: User
Tags bulk insert

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.