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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/////////////////////////////////////////////////////////////////////////////
//    File Description    : DAL中的所有数据访问类,都需要继承BaseDAL
//    Copyright           : joyin
// -------------------------------------------------------------------------
//    Date Created        : Mar 26, 2010
//    Author                : jiangxinjun
// -------------------------------------------------------------------------//
/////////////////////////////////////////////////////////////////////////////
 
#region Namespace
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Reflection;
using HanHe.Utility;
using HanHe.Utility.Data;
using HH.WMS.Entitys.Common;
using HH.WMS.Common;
using HH.Redis.ReisModel;
using HH.WMS.Entitys.Basic;
#endregion
 
namespace HH.WMS.DAL
{
    /// <summary>
    /// 创建Database委托
    /// </summary>
    /// <returns>数据访问工具类实例</returns>
    //public delegate Database CreateDataBaseHandler();
 
    /// <summary>
    /// DAL所有数据访问类的基类
    /// </summary>
    /// <remarks>DAL中的所有数据访问类,都需要继承BaseDAL</remarks>
    public class ErpBaseDAL : BaseDataUtility
    {
        public RedisUserEntity userInfo;
 
 
        public enum DBTYPE
        {
            MYSQL,
            MSSQL,
            ORACLE,
            ORTHER
        }
        public DBTYPE NowDbType
        {
            get
            {
                if (Now_dbType.Equals("MYSQL"))
                    return DBTYPE.MYSQL;
                if (Now_dbType.Equals("MSSQL"))
                    return DBTYPE.MSSQL;
                if (Now_dbType.Equals("ORACLE"))
                    return DBTYPE.ORACLE;
                return DBTYPE.ORTHER;
            }
        }
 
        public string Now_dbType = "MSSQL";
        /// <summary>
        /// 创建Database委托
        /// </summary>
        /// <returns>数据访问工具类实例</returns>
        public delegate Database CreateDataBaseHandler();
 
        #region 初始化
        private Database db;
        private string databaseName;
 
        /// <summary>
        /// 默认的数据库连接字符串在配置文件的名称(根据连接配置获得所要连接的数据库)
        /// </summary>
        internal static readonly string DATABASE_NAME = "erpConnection";
        /// <summary>
        /// 默认根据属性名获取属性对象的策略。
        /// </summary>
        private static BindingFlags PublicInstancePropertyBindingFlags = BindingFlags.CreateInstance
            | BindingFlags.Instance         //实例
            | BindingFlags.Public           //公有
            | BindingFlags.SetProperty      //可写属性
            | BindingFlags.IgnoreCase;      //忽略大小写
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public ErpBaseDAL()
            : this(DATABASE_NAME)
        {
        }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="databaseName">数据库连接字符串在配置文件的名称</param>
        internal ErpBaseDAL(string databaseName)
        {
            this.databaseName = databaseName;
        }
 
        /// <summary>
        /// 创建Database对象
        /// </summary>
        /// <returns>数据访问工具类实例</returns>
        protected override Database CreateDataBase()
        {
            if (db == null)
            {
                db = CreateDataAccess(databaseName);
            }
            return db;
        }
 
        /// <summary>
        /// 获取Database对象
        /// </summary>
        private Database Database
        {
            get
            {
                return CreateDataBase();
            }
        }
        #endregion
 
        #region DataAccess
        /// <summary>
        /// 数据库执行工具类
        /// </summary>
        internal protected new Database DataAccess
        {
            get
            {
                return base.DataAccess;
            }
        }
        #endregion
 
        #region ExecuteInsertWithGetIdentity
        /// <summary>
        /// 执行Insert语句,并且输出新增的自增长值
        /// </summary>
        /// <param name="cmd">Sql命令</param>
        /// <param name="trans">事务对象,如果为null,则不使用事务</param>
        /// <param name="id">如果执行Insert语句成功,则输出自增长值;否则输出0.</param>
        /// <returns>执行结果,参考<see cref="HH.WMS.Entity.SqlExecuteResult"/></returns>
        /// <remarks>如果参数trans不为null的话:调用此方法的过程中,trans是不会做Commit,必须方法外部做Commit;但如果出现异常,trans会执行Rollback。</remarks>
        internal SqlExecuteResult ExecuteInsertWithGetIdentity(DbCommand cmd, DbTransaction trans, out int id)
        {
            SqlExecuteResult result = new SqlExecuteResult();
            id = default(int);
 
            //在Insert语句后面添加获取自增长Id的语句
            cmd.CommandText += ";" + CommonUseSqlFormat.RETRIEVE_NEWID;
 
            if (trans == null)
            {
                try
                {
                    object newid = DataAccess.ExecuteScalar(cmd);
                    if (!IsNullValue(newid))
                    {
                        id = Convert.ToInt32(newid);
                        result.Success = true;
                    }
                }
                catch (Exception ex)
                {
                    result.Success = false;
                    result.Exception = ex;
                }
            }
            else
            {
                try
                {
                    object newid = DataAccess.ExecuteScalar(cmd, trans);
                    if (!IsNullValue(newid))
                    {
                        id = Convert.ToInt32(newid);
                        result.Success = true;
                    }
                }
                catch (Exception ex)
                {
                    result.Success = false;
                    result.Exception = ex;
                    DAOManager.RollbackTransaction(trans);
                }
            }
 
            return result;
        }
        #endregion
 
        #region ExecuteCommand
        /// <summary>
        /// 在事务中,执行Sql命令。
        /// </summary>
        /// <param name="command">Sql命令</param>
        /// <param name="trans">事务对象,如果为null,则不使用事务</param>
        /// <returns>执行结果,参考<see cref="HH.WMS.Entity.SqlExecuteResult"/></returns>
        /// <remarks>如果参数trans不为null的话:调用此方法的过程中,trans是不会做Commit,必须方法外部做Commit;但如果出现异常,trans会执行Rollback。</remarks>
        internal SqlExecuteResult ExecuteCommand(DbCommand command, DbTransaction trans)
        {
            SqlExecuteResult result = new SqlExecuteResult();
 
            if (trans == null)
            {
                //自己创建一个事务执行
                return DAOManager.ExecuteTransaction(this.Database, _trans =>
                {
                    try
                    {
                        result.Row = DataAccess.ExecuteNonQuery(command, _trans);
                        result.Success = true;
                    }
                    catch (Exception ex)
                    {
                        result.Success = false;
                        result.Exception = ex;
                        DAOManager.RollbackTransaction(_trans);
                    }
                    return result;
                });
            }
            else
            {
                try
                {
 
                    result.Row = DataAccess.ExecuteNonQuery(command, trans);
                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Success = false;
                    result.Exception = ex;
                    DAOManager.RollbackTransaction(trans);
                }
            }
 
            return result;
        }
        #endregion
 
        #region ExecuteCommands
        /// <summary>
        /// 在事务中,执行一组Sql命令。
        /// </summary>
        /// <param name="commands">Sql命令列表</param>
        /// <param name="trans">事务对象,如果为null,则不使用事务</param>
        /// <returns>执行结果,参考<see cref="HH.WMS.Entity.SqlExecuteResult"/></returns>
        /// <remarks>如果参数trans不为null的话:调用此方法的过程中,trans是不会做Commit,必须方法外部做Commit;但如果出现异常,trans会执行Rollback。</remarks>
        public SqlExecuteResult ExecuteCommands(List<DbCommand> commands, DbTransaction trans)
        {
            SqlExecuteResult result = new SqlExecuteResult();
 
            if (trans == null)
            {
                //自己创建一个事务执行
                return DAOManager.ExecuteTransaction(this.Database, _trans =>
                {
                    try
                    {
                        foreach (var command in commands)
                        {
                            DataAccess.ExecuteNonQuery(command, _trans);
                        }
 
                        result.Success = true;
                    }
                    catch (Exception ex)
                    {
                        result.Success = false;
                        result.Exception = ex;
                        DAOManager.RollbackTransaction(_trans);
                    }
                    return result;
                });
            }
            else
            {
                try
                {
                    foreach (var command in commands)
                    {
                        DataAccess.ExecuteNonQuery(command, trans);
                    }
                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Success = false;
                    result.Exception = ex;
                    DAOManager.RollbackTransaction(trans);
                }
            }
 
            return result;
        }
        #endregion
 
 
        #region CheckExit
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="columnName">栏位名</param>
        /// <param name="mapperValue">比对的值</param>
        /// <param name="idColumnName">PK的栏位</param>
        /// <param name="result"></param>
        /// <param name="result">输出结果,参考<see cref="HH.WMS.Entity.SqlExecuteResult"/>。如果数据存在,SqlExecuteResult的TokenObject包含了此笔数据的Id值;Exception抛出的异常类型为CheckExistException</param>
 
        internal bool CheckExitObj(string tableName, string columnName, string mapperValue
            , string idColumnName)
        {
            bool isExist = false;
            /*
            * 组合执行以下Sql
            * select {idColumnName} from {tableName} where {columnName} = {mapperValue}
            */
            string sql = string.Format("SELECT {2} FROM {0} WHERE {1} = @val "
                , tableName, columnName, idColumnName);
 
            DbCommand cmd = DataAccess.GetSqlStringCommand(sql);
 
            DataAccess.AddInParameter(cmd, "val", DbType.String, mapperValue);
            object _Obj = DataAccess.ExecuteScalar(cmd);
            if (_Obj != null)
                isExist = true;
 
            return isExist;
        }
        #endregion
 
        #region CheckExist
        /// <summary>
        /// 检查数据是否在数据库中才能在
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="columnName">栏位名</param>
        /// <param name="mapperValue">比对的值</param>
        /// <param name="idColumnName">PK的栏位</param>
        /// <param name="trans">事务</param>
        /// <param name="result">输出结果,参考<see cref="HH.WMS.Entity.SqlExecuteResult"/>。如果数据存在,SqlExecuteResult的TokenObject包含了此笔数据的Id值;Exception抛出的异常类型为CheckExistException</param>
        /// <returns>true,表示数据已存在;false,相关数据不存在。</returns>
        internal bool CheckExist(string tableName, string columnName, string mapperValue
            , string idColumnName, DbTransaction trans
            , out SqlExecuteResult result)
        {
            bool isExist = false;
            result = null;
 
            /*
             * 组合执行以下Sql
             * select {idColumnName} from {tableName} where {columnName} = {mapperValue}
             */
            string sql = string.Format("SELECT {2} FROM {0} WHERE {1} = @val "
                , tableName, columnName, idColumnName);
 
            DbCommand cmd = DataAccess.GetSqlStringCommand(sql);
 
            DataAccess.AddInParameter(cmd, "val", DbType.String, mapperValue);
 
            using (IDataReader dr = (trans == null ? DataAccess.ExecuteReader(cmd) : DataAccess.ExecuteReader(cmd, trans)))
            {
                if (dr.Read())
                {
                    isExist = true;
                    result = new SqlExecuteResult()
                    {
                        Success = false,
                        //检测记录是否存在的异常
                        Exception = new CheckExistException()
                        {
                            Mark = columnName.ToLower()
                        },
                        TokenObject = dr[idColumnName],
                    };
                }
                dr.Close();
 
            }
 
            // object obj = (trans == null ? DataAccess.ExecuteScalar(cmd) : DataAccess.ExecuteScalar(cmd, trans));
            return isExist;
        }
        #endregion
 
        #region SetCommandValue
 
        #region CommandMapper
        /// <summary>
        /// 字段名称映射
        /// </summary>
        internal class CommandMapper
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="colName">字段名称(参数名与字段名一样、属性名为字段名去掉下划线)</param>
            internal CommandMapper(string colName)
                : this(colName, colName, GetPropertyNameByColumnName(colName))
            {
            }
 
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="colName">字段名称</param>
            /// <param name="parameterName">参数名称</param>
            /// <param name="propertyName">属性名称</param>
            internal CommandMapper(string colName, string parameterName, string propertyName)
            {
                this.ColumnName = colName;
                this.ParameterName = parameterName;
                this.PropertyName = propertyName;
            }
 
            /// <summary>
            /// 字段名称
            /// </summary>
            internal string ColumnName { get; private set; }
            /// <summary>
            /// 参数名称
            /// </summary>
            internal string ParameterName { get; private set; }
            /// <summary>
            /// 属性名称
            /// </summary>
            internal string PropertyName { get; private set; }
        }
        #endregion
 
        #region SetCommandValueWithCheck
        /// <summary>
        /// 为DbCommand赋值参数,赋值时添加检测(只检测字符串大小是否大于数据库所设长度)
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="mappers">字段、属性、参数名称的映射关系</param>
        /// <param name="cmd">Command对象</param>
        /// <param name="tableName">Command要设置的相关表名</param>
        /// <returns>错误参数信息,如果没错误返回空集合</returns>
        internal List<CheckInvalidValue> SetCommandValueWithCheck(object entity
            , List<CommandMapper> mappers
            , DbCommand cmd, string tableName)
        {
            List<CheckInvalidValue> list = new List<CheckInvalidValue>();
            Type entityType = entity.GetType();
 
            foreach (var mapper in mappers)
            {
                PropertyInfo pInfo = entityType.GetProperty(mapper.PropertyName, PublicInstancePropertyBindingFlags);
                if (pInfo == null) throw new Exception(entityType.FullName + "类型不存在" + mapper.PropertyName + "属性");
 
                Type pType = pInfo.PropertyType;
                if (pType == typeof(string))
                {
                    //字符串才检验
                    var checkInvalid = SetCommandValueWithCheck(entity
                        , pInfo
                        , mapper.ParameterName
                        , cmd
                        , mapper.ColumnName
                        , tableName);
                    if (checkInvalid != null)
                    {
                        list.Add(checkInvalid);
                    }
                }
                else
                {
                    if (CollectionUti.HasItem(list))
                    {
                        continue;
                    }
                    else
                    {
                        this.SetCommandValue(entity, pInfo, mapper.ParameterName, cmd);
                    }
                }
            }
 
            return list;
        }
 
        /// <summary>
        /// 为DbCommand赋值参数,赋值时添加检测(只检测字符串大小是否大于数据库所设长度)
        /// </summary>
        /// <param name="entity">相关实体</param>
        /// <param name="propertyName">属性名称</param>
        /// <param name="parameterName">要复制的采纳数</param>
        /// <param name="cmd">DbCommand命令</param>
        /// <param name="mapperColName">字段名</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        internal CheckInvalidValue SetCommandValueWithCheck(object entity, string propertyName
            , string parameterName, DbCommand cmd
            , string mapperColName, string tableName)
        {
            Type entityType = entity.GetType();
 
            PropertyInfo pInfo = entityType.GetProperty(propertyName, PublicInstancePropertyBindingFlags);
            if (pInfo == null) throw new Exception(entityType.FullName + "类型不存在" + propertyName + "属性");
 
            return SetCommandValueWithCheck(entity
                , pInfo
                , parameterName
                , cmd
                , mapperColName
                , tableName);
        }
 
        /// <summary>
        /// 为DbCommand赋值参数,赋值时添加检测(只检测字符串大小是否大于数据库所设长度)
        /// </summary>
        /// <param name="entity">相关实体</param>
        /// <param name="pInfo">属性名称</param>
        /// <param name="parameterName">要复制的采纳数</param>
        /// <param name="cmd">DbCommand命令</param>
        /// <param name="mapperColName">字段名</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        internal CheckInvalidValue SetCommandValueWithCheck(object entity, PropertyInfo pInfo
            , string parameterName, DbCommand cmd
            , string mapperColName, string tableName)
        {
            object val = pInfo.GetValue(entity, null);
            CheckInvalidValue checkInvalidValue = null;
 
            mapperColName = mapperColName.ToUpper();
 
            var schema = DAOManager.LoadTableSchema(tableName, this.DataAccess);
            if (!schema.ContainsKey(mapperColName))
            {
                throw new Exception(mapperColName + "在表" + tableName + "中不存在");
            }
            DatabaseColumnStruct dbColumnStruct = schema[mapperColName];
            //字符串才检查
            if (dbColumnStruct.DbType == typeof(string))
            {
                if (!dbColumnStruct.AllowDBNull && (val == null || val.ToString() == ""))
                {
                    //检查为null不合法
                    checkInvalidValue = new CheckInvalidValue()
                    {
                        DBColumnStruct = dbColumnStruct,
                        EntityType = entity.GetType(),
                        PropertyName = pInfo.Name,
                        Value = val,
                        InvalidMessage = "不允许为空"
                    };
                }
 
                if (checkInvalidValue == null && val != null)
                {
                    //检查长度
                    string valString = val.ToString();
                    if (valString.Length > dbColumnStruct.Size)
                    {
                        checkInvalidValue = new CheckInvalidValue()
                        {
                            DBColumnStruct = dbColumnStruct,
                            EntityType = entity.GetType(),
                            PropertyName = pInfo.Name,
                            Value = val,
                            InvalidMessage = "超过数据库预设长度" + dbColumnStruct.Size.ToString()
                        };
                    }
                }
            }
 
            if (checkInvalidValue == null)
            {
                //为DbCommand赋值
                DataAccess.AddInParameter(cmd, parameterName, DbType.String, val ?? (object)DBNull.Value);
            }
 
            return checkInvalidValue;
        }
 
        /// <summary>
        /// 为DbCommand赋值参数,赋值时添加检测(只检测字符串大小是否大于数据库所设长度)
        /// </summary>
        /// <param name="val">值</param>
        /// <param name="parameterName">参数名称</param>
        /// <param name="cmd">Command对象</param>
        /// <param name="mapperColName">对应的字段名称</param>
        /// <param name="tableName">Command要设置的相关表名</param>
        /// <returns>错误参数信息,如果没错误返回null</returns>
        internal CheckInvalidValue SetCommandValueWithCheck(object val, string parameterName
            , DbCommand cmd
            , string mapperColName
            , string tableName)
        {
            CheckInvalidValue checkInvalidValue = null;
 
            mapperColName = mapperColName.ToUpper();
 
            var schema = DAOManager.LoadTableSchema(tableName, this.DataAccess);
            if (!schema.ContainsKey(mapperColName))
            {
                throw new Exception(mapperColName + "在表" + tableName + "中不存在");
            }
            DatabaseColumnStruct dbColumnStruct = schema[mapperColName];
            //字符串才检查
            if (dbColumnStruct.DbType == typeof(string))
            {
                if (!dbColumnStruct.AllowDBNull && (val == null || val.ToString() == ""))
                {
                    //检查为null不合法
                    checkInvalidValue = new CheckInvalidValue()
                    {
                        DBColumnStruct = dbColumnStruct,
                        EntityType = null,
                        PropertyName = null,
                        Value = val,
                        InvalidMessage = "不允许为空"
                    };
                }
 
                if (checkInvalidValue == null && val != null)
                {
                    //检查长度
                    string valString = val.ToString();
                    if (valString.Length > dbColumnStruct.Size)
                    {
                        checkInvalidValue = new CheckInvalidValue()
                        {
                            DBColumnStruct = dbColumnStruct,
                            EntityType = null,
                            PropertyName = null,
                            Value = val,
                            InvalidMessage = "超过数据库预设长度" + dbColumnStruct.Size.ToString()
                        };
                    }
                }
            }
 
            if (checkInvalidValue == null)
            {
                //为DbCommand赋值
                DataAccess.AddInParameter(cmd, parameterName, DbType.String, val ?? (object)DBNull.Value);
            }
 
            return checkInvalidValue;
        }
        #endregion
 
        #region SetCommandValue
        /// <summary>
        /// 为DbCommand赋值参数
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="propName">实体的属性名</param>
        /// <param name="parameterName">参数名</param>
        /// <param name="cmd">DbCommand对象</param>
        internal void SetCommandValue(object entity, string propName, string parameterName, DbCommand cmd)
        {
            PropertyInfo pInfo = entity.GetType().GetProperty(propName);
            SetCommandValue(entity, pInfo, parameterName, cmd);
        }
 
        /// <summary>
        /// 为DbCommand赋值参数
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="pInfo">实体的属性</param>
        /// <param name="parameterName">参数名</param>
        /// <param name="cmd">DbCommand对象</param>
        private void SetCommandValue(object entity, PropertyInfo pInfo, string parameterName, DbCommand cmd)
        {
            Type type = pInfo.PropertyType;
 
            object val = pInfo.GetValue(entity, null);
 
            if (type.IsEnum)
            {
                val = (int)val;
                type = typeof(int);
            }
 
            if (type == typeof(int))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.Int32, val);
            }
            else if (type == typeof(decimal))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.Decimal, val);
            }
            else if (type == typeof(DateTime))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.DateTime, val);
            }
            else if (type == typeof(double))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.Double, val);
            }
            else if (type == typeof(long))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.Int64, val);
            }
            else if (type == typeof(int?))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.Int32, GetNullableDbValue<int>((int?)val));
            }
            else if (type == typeof(decimal?))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.Decimal, GetNullableDbValue<decimal>((decimal?)val));
            }
            else if (type == typeof(DateTime?))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.DateTime, GetNullableDbValue<DateTime>((DateTime?)val));
            }
            else if (type == typeof(double?))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.Double, GetNullableDbValue<double>((double?)val));
            }
            else if (type == typeof(long?))
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.Int64, GetNullableDbValue<long>((long?)val));
            }
            else
            {
                DataAccess.AddInParameter(cmd, parameterName, DbType.String, val);
            }
        }
        #endregion
 
        #endregion
 
        #region GetPropertyNameByColumnName
        /// <summary>
        /// 根据字段名称,寻找属性名称的默认规则
        /// </summary>
        /// <param name="colName">字段名称</param>
        /// <returns>返回实体属性名称</returns>
        internal static string GetPropertyNameByColumnName(string colName)
        {
            return colName.Replace("_", "");
        }
        #endregion
 
        #region SetEntityUti
        /// <summary>
        /// 创建SetEntity的委托
        /// </summary>
        /// <typeparam name="T">实体的类型</typeparam>
        /// <returns></returns>
        internal Action<T, IDataReader> CreateSetEntityHandler<T>()
            where T : BaseEntity
        {
            return new Action<T, IDataReader>((_entity, _reader) =>
            {
                SetEntityUti(_entity, _reader);
            });
        }
 
        /// <summary>
        /// 从DataReader获取数据到实体
        /// </summary>
        /// <param name="entity">实体,不允许为空</param>
        /// <param name="dr">DataReader对象,不允许为空</param>
        internal void SetEntityUti(object entity, IDataReader dr)
        {
            this.SetEntityUti(entity, dr, delegate
            {
                return true;
            });
        }
 
        /// <summary>
        /// 从DataReader获取数据到实体
        /// </summary>
        /// <param name="entity">实体,不允许为空</param>
        /// <param name="dr">DataReader对象,不允许为空</param>
        /// <param name="mapper">属性与字段名称的映射,Key:为字段名,必须都为大写;Value为属性名</param>
        internal void SetEntityUti(object entity, IDataReader dr, Dictionary<string, string> mapper)
        {
            SetEntityUti(entity, dr, (_pInfo, _colName) =>
            {
                if (mapper.ContainsKey(_colName.ToUpper()))
                {
                    SetEntityUti(entity, mapper[_colName.ToUpper()], _colName, dr);
                    return false;
                }
                return true;
            });
        }
 
        /// <summary>
        /// 从DataReader获取数据到实体
        /// </summary>
        /// <param name="entity">实体,不允许为空</param>
        /// <param name="dr">DataReader对象,不允许为空</param>
        /// <param name="action">特殊处理,返回true时,就函数默认处理;返回false,函数就不处理</param>
        internal void SetEntityUti(object entity, IDataReader dr, Func<PropertyInfo, string, bool> action)
        {
            Type entityType = entity.GetType();
            //循环dr的所有字段
            int fieldCount = dr.FieldCount;
            for (int fieldIndex = 0; fieldIndex < fieldCount; fieldIndex++)
            {
                //根据字段名称转换为属性名
                string colName = dr.GetName(fieldIndex);
                string propertyName = GetPropertyNameByColumnName(colName);
                PropertyInfo pInfo = entityType.GetProperty(propertyName, PublicInstancePropertyBindingFlags);
 
                if (action(pInfo, colName) && pInfo != null)
                {
                    SetEntityUti(entity, pInfo, colName, dr);
                }
            }
        }
 
        /// <summary>
        /// 从DataReader获取数据到实体
        /// </summary>
        /// <param name="entity">实体,不允许为空</param>
        /// <param name="propName">属性名,不允许为空</param>
        /// <param name="colName">字段名,不允许为空</param>
        /// <param name="dr">DataReader对象,不允许为空</param>
        /// <remarks>一般不建议使用,效率问题</remarks>
        internal void SetEntityUti(object entity, string propName, string colName, IDataReader dr)
        {
            if (string.IsNullOrEmpty(propName)) throw new NullReferenceException("pInfo不允许为空");
            if (entity == null) throw new NullReferenceException("entity不允许为空");
 
            PropertyInfo pInfo = entity.GetType().GetProperty(propName, PublicInstancePropertyBindingFlags);
            if (pInfo == null)
            {
                throw new Exception(propName + "属性在" + entity.GetType().FullName + "不存在");
            }
            SetEntityUti(entity, pInfo, colName, dr);
        }
 
        /// <summary>
        /// 从DataReader获取数据到实体
        /// </summary>
        /// <param name="entity">实体,不允许为空</param>
        /// <param name="pInfo">属性对象,不允许为空</param>
        /// <param name="colName">字段名,不允许为空</param>
        /// <param name="dr">DataReader对象,不允许为空</param>
        /// <remarks>一般不建议使用,效率问题</remarks>
        internal void SetEntityUti(object entity, PropertyInfo pInfo, string colName, IDataReader dr)
        {
            if (pInfo == null) throw new NullReferenceException("pInfo不允许为空");
            if (entity == null) throw new NullReferenceException("entity不允许为空");
 
            Type pType = pInfo.PropertyType;
 
            if (pType == typeof(int))
            {
                pInfo.SetValue(entity, GetDataByDataReader<int>(dr, colName, DefaultData.INT), null);
            }
            else if (pType == typeof(decimal))
            {
                pInfo.SetValue(entity, GetDataByDataReader<decimal>(dr, colName, DefaultData.DECIMAL), null);
            }
            else if (pType == typeof(DateTime))
            {
                pInfo.SetValue(entity, GetDataByDataReader<DateTime>(dr, colName, DefaultData.DATETIME), null);
            }
            else if (pType == typeof(long))
            {
                pInfo.SetValue(entity, GetDataByDataReader<long>(dr, colName, DefaultData.LONG), null);
            }
            else if (pType == typeof(int?))
            {
                pInfo.SetValue(entity, GetNullableDataByDataReader<int>(dr, colName), null);
 
            }
            else if (pType == typeof(decimal?))
            {
                pInfo.SetValue(entity, GetNullableDataByDataReader<decimal>(dr, colName), null);
 
            }
            else if (pType == typeof(DateTime?))
            {
                pInfo.SetValue(entity, GetNullableDataByDataReader<DateTime>(dr, colName), null);
 
            }
            else if (pType == typeof(Boolean))
            {
                pInfo.SetValue(entity, GetNullableDataByDataReader<Boolean>(dr, colName), null);
 
            }
            else if (pType == typeof(String))
            {
                if (!string.IsNullOrEmpty(GetDataByDataReader<String>(dr, colName, DefaultData.STRING).Trim()))
                {
                    pInfo.SetValue(entity, GetDataByDataReader<String>(dr, colName, DefaultData.STRING).Trim(), null);
                }
                else
                {
                    pInfo.SetValue(entity, GetDataByDataReader<String>(dr, colName, DefaultData.STRING), null);
                }
            }
            else if (pType == typeof(long?))
            {
                pInfo.SetValue(entity, GetNullableDataByDataReader<long>(dr, colName), null);
            }
            else
            {
                pInfo.SetValue(entity, GetTrimString(dr, colName).Trim(), null);
            }
        }
        #endregion
 
        #region GetNullableDbValue
        /// <summary>
        /// 把可空值的数据转成Db数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="val"></param>
        /// <returns></returns>
        internal static object GetNullableDbValue<T>(Nullable<T> val)
            where T : struct
        {
            if (val.HasValue)
            {
                return val.Value;
            }
 
            return DBNull.Value;
        }
        #endregion
 
        #region 可供自定义创建Database
        /// <summary>
        /// 【提供给UnitTest使用】Be convenient for changing the ConnectionString in test.
        /// </summary>
        private static CreateDataBaseHandler createDataBaseHandler;
 
        /// <summary>
        /// 【提供给UnitTest使用】Change the default connection string
        /// </summary>
        /// <param name="handler"></param>
        public static void SetCreateDataBaseHandler(CreateDataBaseHandler handler)
        {
            createDataBaseHandler = handler;
        }
        #endregion
 
        #region CreateDataAccess
        /// <summary>
        /// 获取配置文件中的数据库字符串信息,并解密
        /// </summary>
        /// <param name="databaseName"></param>
        /// <returns></returns>
        public static Database CreateDataAccess(string databaseName)
        {
            Database database = null;
            if (createDataBaseHandler == null)
            {
                //读取Web.Config
                var config = ConfigurationManager.ConnectionStrings[databaseName];
                //config.ConnectionString = ConnectionStringCryptography.Decrypt(config.ConnectionString);
 
                //解密字符串
                ConnectionStringSettings csCopy = new ConnectionStringSettings();
                // csCopy.ConnectionString = ConnectionStringCryptography.Decrypt(config.ConnectionString);
                csCopy.ConnectionString = config.ConnectionString;
                csCopy.ProviderName = config.ProviderName;
                csCopy.Name = config.Name;
 
                database = DatabaseFactory.CreateDatabase(csCopy);
 
            }
            else
            {
                database = createDataBaseHandler();
            }
            return database;
        }
        #endregion
    }
}