From 560ec58908f80486c43dcb794d3d3762b9858419 Mon Sep 17 00:00:00 2001 From: wanli Date: Tue, 3 Jan 2023 22:29:29 +0800 Subject: [PATCH] =?UTF-8?q?[Repository]=20=E5=A2=9E=E5=8A=A0=E6=B8=85?= =?UTF-8?q?=E7=A9=BA=E8=B3=87=E6=96=99=E8=A1=A8=20=E5=87=BD=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implement/BaseRepository.cs | 36 +++++++++++++++++++ .../Interface/IBaseRepository.cs | 7 ++++ 2 files changed, 43 insertions(+) diff --git a/Repository/BaseRepository/Implement/BaseRepository.cs b/Repository/BaseRepository/Implement/BaseRepository.cs index e0b50cd..0de7f78 100644 --- a/Repository/BaseRepository/Implement/BaseRepository.cs +++ b/Repository/BaseRepository/Implement/BaseRepository.cs @@ -888,5 +888,41 @@ namespace Repository.BaseRepository.Implement } #endregion AddOneReturnId + + #region TruncateTable (清空資料表) + /// + /// 清空資料表資料 + /// TRUNCATE TABLE {tableName} + /// + /// + /// + public virtual async Task TruncateTable(string table_name) + { + using (IDbConnection conn = GetDbConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = $"TRUNCATE TABLE {table_name}"; + await conn.ExecuteAsync(sql, null, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } + #endregion TruncateTable + } } diff --git a/Repository/BaseRepository/Interface/IBaseRepository.cs b/Repository/BaseRepository/Interface/IBaseRepository.cs index c4ad4df..1c041a0 100644 --- a/Repository/BaseRepository/Interface/IBaseRepository.cs +++ b/Repository/BaseRepository/Interface/IBaseRepository.cs @@ -181,5 +181,12 @@ namespace Repository.BaseRepository.Interface /// /// Task AddOneByCustomTableReturnId(Dictionary dict, string Table_name, bool returnId = true); + /// + /// 清空table資料 + /// + /// 資料表名稱 + /// + /// + Task TruncateTable(string table_name); } }