C#/Winform

[C#/Dapper] Bulk Insert (List<T> Insert)

kjun.kr 2022. 4. 23. 23:34
728x90
728x170
using (SqlConnection connection = new SqlConnection(this.connectionString))
{
    connection.Open();

    // 데이터 삭제
    connection.Execute("DELETE FROM TestTable");

    // 추가될 데이터
    List<TestDataModel> testDataList = new List<TestDataModel>();

    for (int i = 0; i < 15; i++)
    {
        testDataList.Add(new TestDataModel() { ID = i, DATA1 = "A" + i, DATA2 = "B" + i, DATA3 = "C" + i });
    }

    SqlTransaction transaction = connection.BeginTransaction();
    try
    {
        connection.Execute("INSERT TestTable (DATA1, DATA2, DATA3) VALUES (@DATA1, @DATA2, @DATA3)", testDataList, transaction);
        transaction.Commit();
    }
    catch(Exception ex)
    {
        transaction.Rollback();
        throw ex;
    }

실제 bulk insert 는 Dapper 유료버전에 있다; 위처럼 사용하면 여러 데이터 처리가 가능하다.

728x90
그리드형