C#/Winform

[C#/Dapper] Multi Delete, Multi Update

kjun.kr 2022. 4. 23. 23:43
728x90
728x170

쿼리문 하나에 parameter 를 변경해 일일히 쿼리하는 일을 아래처럼 하면 dapper 가 알아서 해준다.

#region MultiUpdate
/// <summary>
/// Multipul Update
/// </summary>
private void MultiUpdate()
{
    string sql = "UPDATE TestTable SET DATA1 = @DATA1 WHERE ID = @ID;";

    using (IDbConnection db = new SqlConnection(this.connectionString))
    {
        int rowsAffected = db.Execute(sql,
            new[]
            {
                    new { ID = 1, DATA1 = "UPDATEA1" },
                    new { ID = 2, DATA1 = "UPDATEA2" },
            }
        );
    }
}
#endregion
#region MultiDelete
/// <summary>
/// Multipul Delete
/// </summary>
private void MultiDelete()
{
    string sql = "DELETE FROM TestTable WHERE ID = @ID;";

    using (IDbConnection db = new SqlConnection(this.connectionString))
    {
        int rowsAffected = db.Execute(sql,
            new[]
            {
                    new { Id = 1 },
                    new { Id = 2 }
            }
        );
    }
}
#endregion
728x90
그리드형