■ SqlBulkCopy 클래스 : 대용량 데이터 추가하기

----------------------------------------------------------------------------------------------------

using System.Data;

using System.Data.SqlClient;

 

#region 대용량 데이터 추가하기 - BulkInsert(connectionString, targetTableName, sourceTable, batchSize)

 

/// <summary>

/// 대용량 데이터 추가하기

/// </summary>

/// <param name="connectionString">연결 문자열</param>

/// <param name="targetTableName">타겟 테이블명</param>

/// <param name="sourceTable">소스 테이블</param>

/// <param name="batchSize">배치 크기</param>

public void BulkInsert(string connectionString, string targetTableName, DataTable sourceTable, int batchSize)

{

    using(SqlConnection sqlConnection = new SqlConnection(connectionString))

    {

        sqlConnection.Open();

 

        using(SqlTransaction sqlTransaction = sqlConnection.BeginTransaction())

        {

            SqlBulkCopy sqlBulkCopy = new SqlBulkCopy

            (

                sqlConnection,

                SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.KeepNulls,

                sqlTransaction

            );

 

            sqlBulkCopy.DestinationTableName = targetTableName;

            sqlBulkCopy.BatchSize            = batchSize;

 

            sqlBulkCopy.WriteToServer(sourceTable);

 

            sqlTransaction.Commit();

        }

 

        sqlConnection.Close();

    }

}

 

#endregion

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요