From 7a0d98456b4ecaf1867cd62ecb7c8169ec15d042 Mon Sep 17 00:00:00 2001
From: yl <ykxkd@outlook.com>
Date: 星期三, 22 六月 2022 18:20:34 +0800
Subject: [PATCH] 设备清单查询、新增、编辑、删除接口开发 设备类型(新增、删除)、设备组(新增、删除)接口开发 仓库定义(新增、编辑、删除)、库位定义(新增、编辑、删除) 存货类型、存货档案接口开发

---
 VueWebApi/Tools/DapperHelper.cs |  202 +++++++++++++++++++++++++++++++------------------
 1 files changed, 127 insertions(+), 75 deletions(-)

diff --git a/VueWebApi/Tools/DapperHelper.cs b/VueWebApi/Tools/DapperHelper.cs
index 55616c7..132be16 100644
--- a/VueWebApi/Tools/DapperHelper.cs
+++ b/VueWebApi/Tools/DapperHelper.cs
@@ -52,7 +52,12 @@
                     LogHelper.WriteLog(ex);
                     throw ex;
                 }
-                
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
+
             }
             return list;
         }
@@ -81,6 +86,11 @@
                     LogHelper.WriteLog(ex);
                     throw ex;
                 }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
             }
             return result;
         }
@@ -108,6 +118,11 @@
                     LogHelper.WriteLog(ex);
                     throw ex;
                 }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
             }
             return t;
         }
@@ -122,25 +137,24 @@
         /// <param name="pageSize">褰撳墠椤垫樉绀烘潯鏁�</param>
         /// <param name="total">缁撴灉闆嗘�绘暟</param>
         /// <returns></returns>
-        public static IEnumerable<T> GetPageList<T>(string sql,Object parm, string orderBy,string sort, int pageIndex, int pageSize, out int total)
+        public static IEnumerable<T> GetPageList<T>(string sql, Object parm, string orderBy, string sort, int pageIndex, int pageSize, out int total)
         {
-            try
-            {
-                //int skip = 1;
-                //if (pageIndex > 0)
-                //{
-                //    skip = (pageIndex - 1) * pageSize + 1;
-                //}
-
-                StringBuilder sb = new StringBuilder();
-                sb.AppendFormat("SELECT COUNT(1) FROM ({0}) AS Result;", sql);
-                sb.AppendFormat(@"SELECT *
+            //int skip = 1;
+            //if (pageIndex > 0)
+            //{
+            //    skip = (pageIndex - 1) * pageSize + 1;
+            //}
+            StringBuilder sb = new StringBuilder();
+            sb.AppendFormat("SELECT COUNT(1) FROM ({0}) AS Result;", sql);
+            sb.AppendFormat(@"SELECT *
                         FROM(SELECT *,ROW_NUMBER() OVER(ORDER BY {1} {2}) AS RowNum
                              FROM  ({0}) AS Temp) AS Result
                         WHERE  RowNum >= {3} AND RowNum <= {4}
                         ORDER BY {1} {2}", sql, orderBy, sort, pageIndex, pageSize);  //skip, pageIndex * pageSize
 
-                using (IDbConnection conn = sqlConnection())
+            using (IDbConnection conn = sqlConnection())
+            {
+                try
                 {
                     using (var reader = conn.QueryMultiple(sb.ToString(), parm))
                     {
@@ -148,14 +162,19 @@
                         return reader.Read<T>();
                     }
                 }
+                catch (Exception ex)
+                {
+                    //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
+                    LogHelper.WriteLog(ex);
+                    throw ex;
+                }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
+
             }
-            catch (Exception ex)
-            {
-                //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
-                LogHelper.WriteLog(ex);
-                throw ex;
-            }
-           
         }
 
         /// <summary>
@@ -167,23 +186,27 @@
         public static DataTable selectdata(string sql, Object parm)
         {
             //sql璇彞
-            try
+            using (IDbConnection conn = sqlConnection())
             {
-                using (IDbConnection conn = sqlConnection())
+                try
                 {
                     DataTable table = new DataTable();
                     var data = conn.ExecuteReader(sql, parm);
                     table.Load(data);
                     return table;
                 }
+                catch (Exception ex)
+                {
+                    //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
+                    LogHelper.WriteLog(ex);
+                    throw ex;
+                }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
             }
-            catch (Exception ex)
-            {
-                //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
-                LogHelper.WriteLog(ex);
-                throw ex;
-            }
-           
         }
 
         /// <summary>
@@ -194,23 +217,27 @@
         public static DataTable selecttable(string sql)
         {
             //sql璇彞
-            try
+            using (IDbConnection conn = sqlConnection())
             {
-                using (IDbConnection conn = sqlConnection())
+                try
                 {
                     DataTable table = new DataTable();
                     var data = conn.ExecuteReader(sql);
                     table.Load(data);
                     return table;
                 }
+                catch (Exception ex)
+                {
+                    //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
+                    LogHelper.WriteLog(ex);
+                    throw ex;
+                }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
             }
-            catch (Exception ex)
-            {
-                //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
-                LogHelper.WriteLog(ex);
-                throw ex;
-            }
-
         }
 
         /// <summary>
@@ -222,9 +249,9 @@
         public static DataTable selectProcedure(string sql, Object parm)
         {
             //sql璇彞
-            try
+            using (IDbConnection conn = sqlConnection())
             {
-                using (IDbConnection conn = sqlConnection())
+                try
                 {
                     DataTable table = new DataTable();
                     //var data = conn.ExecuteReader(sql, parm);
@@ -232,12 +259,17 @@
                     table.Load(res);
                     return table;
                 }
-            }
-            catch (Exception ex)
-            {
-                //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
-                LogHelper.WriteLog(ex);
-                throw ex;
+                catch (Exception ex)
+                {
+                    //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
+                    LogHelper.WriteLog(ex);
+                    throw ex;
+                }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
             }
 
         }
@@ -252,19 +284,24 @@
         {
             //sql璇彞
             int result = 0;
-            try
+            using (IDbConnection conn = sqlConnection())
             {
-                using (IDbConnection conn = sqlConnection())
+                try
                 {
                     result = conn.Query(sql, parm).Count();
                 }
+                catch (Exception ex)
+                {
+                    //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
+                    LogHelper.WriteLog(ex);
+                    throw ex;
+                }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
                 return result;
-            }
-            catch (Exception ex)
-            {
-                //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
-                LogHelper.WriteLog(ex);
-                throw ex;
             }
         }
 
@@ -274,24 +311,29 @@
         /// <param name="sql"></param>
         /// <param name="parametere"></param>
         /// <returns></returns>
-        public static int SQL(string sql,Object parametere)
+        public static int SQL(string sql, Object parametere)
         {
             //sql璇彞
             int result = 0;
-            try
+            using (IDbConnection conn = sqlConnection())
             {
-                using (IDbConnection conn = sqlConnection())
+                try
                 {
                     result = conn.Execute(sql, parametere);
                 }
-                return result;
+                catch (Exception ex)
+                {
+                    //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
+                    LogHelper.WriteLog(ex);
+                    throw ex;
+                }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
             }
-            catch (Exception ex)
-            {
-                //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
-                LogHelper.WriteLog(ex);
-                throw ex;
-            }   
+            return result;
         }
 
         /// <summary>
@@ -310,7 +352,7 @@
                     tranction = conn.BeginTransaction();
                     for (int i = 0; i < sqlList.Count; i++)
                     {
-                        conn.Execute(sqlList[i].GetType().GetProperty("str").GetValue(sqlList[i], null).ToString(), sqlList[i].GetType().GetProperty("parm").GetValue(sqlList[i], null),tranction);
+                        conn.Execute(sqlList[i].GetType().GetProperty("str").GetValue(sqlList[i], null).ToString(), sqlList[i].GetType().GetProperty("parm").GetValue(sqlList[i], null), tranction);
                     }
                     tranction.Commit();
                     result = true;
@@ -322,6 +364,11 @@
                     result = false;
                     tranction.Rollback();
                     throw ex;
+                }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
                 }
                 return result;
             }
@@ -339,9 +386,9 @@
         {
 
             int result = 0;
-            try
+            using (IDbConnection conn = sqlConnection())
             {
-                using (IDbConnection conn = sqlConnection())
+                try
                 {
                     result = conn.Execute(sql, parameter);
                     if (result != 0)
@@ -349,14 +396,19 @@
                         result = conn.Query<int>("select ident_current(@table)", new { table = tableName }).Single<int>();
                     }
                 }
-                return result;
+                catch (Exception ex)
+                {
+                    //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
+                    LogHelper.WriteLog(ex);
+                    throw ex;
+                }
+                finally
+                {
+                    conn.Dispose();
+                    conn.Close();
+                }
             }
-            catch (Exception ex)
-            { 
-                //鍒涘缓鏃ュ織璁板綍缁勪欢瀹炰緥
-                LogHelper.WriteLog(ex);
-                throw ex;
-            }
+            return result;
         }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3