Use bulk storage for data SqlBulkCopy

Field correspondence datatable fields in the dataset in the database

#region the SqlBulkCopy DataTable data into the database in bulk  
         ///  <Summary>   
        /// the SqlBulkCopy DataTable data into the database in bulk  
         ///  </ Summary>   
        ///  <param name = "strTableName" > corresponding database table name </ param>   
        ///  <param name = "DTDATA"> data sets </ param>   
        public  static  int InsertCopy ( the this the SqlConnection Conn, String strTableName, the DataTable DTDATA, the DbTransaction Transaction = null )
        {
            SqlBulkCopy sqlRevdBulkCopy = transaction == null ?
                new SqlBulkCopy(conn) :
                new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, (SqlTransaction)transaction);

            sqlRevdBulkCopy.BulkCopyTimeout = 8000;
            try
            {
                if (conn.State == ConnectionState.Closed) { conn.Open(); }
                sqlRevdBulkCopy.DestinationTableName = strTableName; // database table name corresponding 
                sqlRevdBulkCopy.NotifyAfter = dtData.Rows.Count; // a few rows 
                sqlRevdBulkCopy.WriteToServer (DTDATA); // data into the database

                return dtData.Rows.Count;
            }
            catch (Exception ex)
            {
                Wathet.Log.Error (ex.ToSimple ( " DoSql.InsertCopy, table [ " + strTableName + " ] data relocation exception SqlBulkCopyInsert " ));
                 return - . 1 ;
            }
            finally
            {
                sqlRevdBulkCopy.Close();

                if (transaction == null && conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        #endregion

 

Guess you like

Origin www.cnblogs.com/dujian123/p/11097852.html