728x90
반응형

쿼리문 하나에 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
반응형

'C# > Winform' 카테고리의 다른 글

[C#] gRPC Stream Server/Client 만들기  (0) 2022.05.04
[C#] gRPC Server/Client 만들기  (0) 2022.05.04
[C#/Dapper] Multi Select  (0) 2022.04.23
[C#/Dapper] Bulk Insert (List<T> Insert)  (0) 2022.04.23
[C#/Dapper] Dynamic Parameter  (0) 2022.04.23
Posted by kjun

댓글을 달아 주세요