jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
namespace HW.Utility.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using HW.Utility;
 
    public static class DataAccessExtensive
    {
        public static DataTable ExecuteDataTable(Database dataAccess, DbCommand cmd)
        {
            return ExecuteDataTable(dataAccess, cmd, null);
        }
 
        public static DataTable ExecuteDataTable(Database dataAccess, DbCommand cmd, DbTransaction trans)
        {
            DataTable table = new DataTable();
            DataSet set = (trans != null) ? dataAccess.ExecuteDataSet(cmd, trans) : dataAccess.ExecuteDataSet(cmd);
            if ((set != null) && (set.Tables.Count > 0))
            {
                table = set.Tables[0];
            }
            return table;
        }
 
        public static List<T> ExecuteListEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action) where T: class, new()
        {
            return ExecuteListEntity<T>(dataAccess, cmd, action, null);
        }
 
        public static List<T> ExecuteListEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action, DbTransaction trans) where T: class, new()
        {
            List<T> list = new List<T>();
            if (action != null)
            {
                using (IDataReader reader = (trans == null) ? dataAccess.ExecuteReader(cmd) : dataAccess.ExecuteReader(cmd, trans))
                {
                    while (reader.Read())
                    {
                        T local = Activator.CreateInstance<T>();
                        action(local, reader);
                        list.Add(local);
                    }
                }
            }
            return list;
        }
 
        public static DataRow ExecuteSingleData(Database dataAccess, DbCommand cmd)
        {
            return ExecuteSingleData(dataAccess, cmd, null);
        }
 
        public static DataRow ExecuteSingleData(Database dataAccess, DbCommand cmd, DbTransaction trans)
        {
            DataRow row = null;
            DataTable table = ExecuteDataTable(dataAccess, cmd, trans);
            if ((table != null) && (table.Rows.Count > 0))
            {
                row = table.Rows[0];
            }
            return row;
        }
 
        public static T ExecuteSingleEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action) where T: class, new()
        {
            return ExecuteSingleEntity<T>(dataAccess, cmd, action, null);
        }
 
        public static T ExecuteSingleEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action, DbTransaction trans) where T: class, new()
        {
            T local = default(T);
            if (action != null)
            {
                using (IDataReader reader = (trans == null) ? dataAccess.ExecuteReader(cmd) : dataAccess.ExecuteReader(cmd, trans))
                {
                    if (reader.Read())
                    {
                        local = Activator.CreateInstance<T>();
                        action(local, reader);
                    }
                }
            }
            return local;
        }
 
        public static bool ExecuteTransaction(Database dataAccess, TransactionAction action, Action<Exception> logAction)
        {
            bool flag = false;
            if (action != null)
            {
                using (DbConnection connection = dataAccess.CreateConnection())
                {
                    connection.Open();
                    DbTransaction trans = connection.BeginTransaction();
                    try
                    {
                        if (action(trans))
                        {
                            trans.Commit();
                        }
                        else
                        {
                            trans.Rollback();
                        }
                        flag = true;
                    }
                    catch (Exception exception)
                    {
                        if (logAction != null)
                        {
                            logAction(exception);
                        }
                        trans.Rollback();
                        throw exception;
                    }
                    connection.Close();
                }
            }
            return flag;
        }
    }
}