1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
#region
 
using System;
using System.Collections;
using System.Data;
using System.Reflection;
 
#endregion
 
namespace CSFrameworkV5.Common
{
    /// <summary>
    ///     DataTable-Object 互转换工具
    /// </summary>
    public class DataConverter
    {
        /// <summary>
        ///     新增一条记录
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="o">ORM模型</param>
        /// <returns></returns>
        public static DataRow AddDataRowFromObject(DataTable dt, object o)
        {
            var row = dt.NewRow();
            var pinfo = o.GetType().GetProperties();
            foreach (var info in pinfo)
                if (dt.Columns.IndexOf(info.Name) >= 0)
                    SetDataRowValue(row, info.Name, info.GetValue(o, null));
 
            dt.Rows.Add(row);
            return row;
        }
 
        /// <summary>
        ///     把一个行的数据新增一个表中
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static DataTable AddTableRowByRow(DataTable dt, DataRow dr)
        {
            var b = false;
            var drNew = dt.NewRow();
            for (var i = 0; i < dr.Table.Columns.Count; i++)
            {
                var colname = dr.Table.Columns[i].ColumnName;
                if (dt.Columns.IndexOf(colname) >= 0)
                {
                    drNew[colname] = dr[colname];
                    b = true;
                }
            }
 
            if (b) dt.Rows.Add(drNew);
 
            return dt;
        }
 
        /// <summary>
        ///     根据对象的属性创建数据表
        /// </summary>
        private static DataTable BuiltTable(PropertyInfo[] pinfo)
        {
            try
            {
                if (pinfo == null) return null;
 
                var table = new DataTable();
                foreach (var info in pinfo)
                {
                    var type = info.PropertyType;
                    if (info.PropertyType.IsGenericType)
                        type = info.PropertyType.GetGenericArguments()[0];
 
                    var column = new DataColumn(info.Name, type);
                    column.AllowDBNull = true;
                    table.Columns.Add(column);
                }
 
                return table;
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        ///     指定参数是否可用于浅拷贝
        /// </summary>
        private static bool CanShallowCopyProperty(object propValue)
        {
            if (propValue == null) return true;
 
            if (propValue.GetType().IsValueType || propValue is string)
                return true;
 
            return false;
        }
 
        /// <summary>
        ///     复制对象. 浅拷贝.
        /// </summary>
        public static object CloneObject(object source)
        {
            try
            {
                if (source == null) return null;
 
                var objType = source.GetType();
                var destObj = objType.Assembly.CreateInstance(objType.FullName);
                var propsSource = objType.GetProperties();
                foreach (var infoSource in propsSource)
                {
                    var value = GetValueOfObject(source, infoSource.Name);
                    if (value != null && CanShallowCopyProperty(value))
                        SetPropertyValue(destObj, infoSource, value);
                }
 
                return destObj;
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        ///     复制一个对象数组.
        /// </summary>
        public static ArrayList CloneObjects(IList source)
        {
            if (source == null) return null;
 
            var ret = new ArrayList();
            foreach (var o in source) ret.Add(CloneObject(o));
 
            return ret;
        }
 
        ///// <summary>
        ///// SQL SERVER数据类型(如:varchar)转换为DbType类型
        ///// </summary>
        ///// <param name="sqlTypeString">SQL SERVER数据类型</param>
        ///// <returns>返回DbType类型</returns>
        //public static DbType SqlTypeString2SqlType(string sqlTypeString)
        //{
        //    DbType dbType = DbType.Object;//默认为Object
 
        //    switch (sqlTypeString)
        //    {
        //        case "int":
        //            dbType = DbType.Int32;
        //            break;
        //        case "varchar":
        //            dbType = DbType.String;
        //            break;
        //        case "bit":
        //            dbType = DbType.Boolean;
        //            break;
        //        case "datetime":
        //            dbType = DbType.DateTime;
        //            break;
        //        case "decimal":
        //            dbType = DbType.Decimal;
        //            break;
        //        case "float":
        //            dbType = DbType.Float;
        //            break;
        //        case "image":
        //            dbType = DbType.Binary;
        //            break;
        //        case "money":
        //            dbType = DbType.Money;
        //            break;
        //        case "ntext":
        //            dbType = DbType.NText;
        //            break;
        //        case "nvarchar":
        //            dbType = DbType.String;
        //            break;
        //        case "smalldatetime":
        //            dbType = DbType.SmallDateTime;
        //            break;
        //        case "smallint":
        //            dbType = DbType.SmallInt;
        //            break;
        //        case "text":
        //            dbType = DbType.Text;
        //            break;
        //        case "bigint":
        //            dbType = DbType.BigInt;
        //            break;
        //        case "binary":
        //            dbType = DbType.Binary;
        //            break;
        //        case "char":
        //            dbType = DbType.String;
        //            break;
        //        case "nchar":
        //            dbType = DbType.NChar;
        //            break;
        //        case "numeric":
        //            dbType = DbType.Decimal;
        //            break;
        //        case "real":
        //            dbType = DbType.Real;
        //            break;
        //        case "smallmoney":
        //            dbType = DbType.SmallMoney;
        //            break;
        //        case "sql_variant":
        //            dbType = DbType.Variant;
        //            break;
        //        case "timestamp":
        //            dbType = DbType.Timestamp;
        //            break;
        //        case "tinyint":
        //            dbType = DbType.TinyInt;
        //            break;
        //        case "uniqueidentifier":
        //            dbType = DbType.UniqueIdentifier;
        //            break;
        //        case "varbinary":
        //            dbType = DbType.VarBinary;
        //            break;
        //        case "xml":
        //            dbType = DbType.Xml;
        //            break;
        //    }
        //    return dbType;
        //}
 
        /// <summary>
        ///     数据表是否存在指定的列(Column)
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="columnName">列的名称</param>
        /// <returns></returns>
        public static bool ColumnExists(DataTable dt, string columnName)
        {
            if (dt == null) return false;
 
            foreach (DataColumn col in dt.Columns)
                if (col.ColumnName.ToLower() == columnName.ToLower())
                    return true;
 
            return false;
        }
 
        /// <summary>
        ///     Object to Object. 将一个对象转换为指定类型的对象.
        ///     注意: destination内的Property必需在source内存在.
        /// </summary>
        public static object CopyProperties(object source, Type destination)
        {
            try
            {
                if (source == null) return null;
 
                var destObj =
                    destination.Assembly.CreateInstance(destination.FullName);
                var propsDest = destObj.GetType().GetProperties();
                foreach (var infoDest in propsDest)
                {
                    var value = GetValueOfObject(source, infoDest.Name);
                    if (value != null && CanShallowCopyProperty(value))
                        SetPropertyValue(destObj, infoDest, value);
                }
 
                return destObj;
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        ///     复制对象属性.
        /// </summary>
        public static void CopyProperties(object source, object destObj)
        {
            try
            {
                if (source == null || destObj == null) return;
 
                var propsDest = destObj.GetType().GetProperties();
                foreach (var infoDest in propsDest)
                {
                    var value = GetValueOfObject(source, infoDest.Name);
                    if (value != null && CanShallowCopyProperty(value))
                        SetPropertyValue(destObj, infoDest, value);
                }
            }
            catch
            {
            }
        }
 
        /// <summary>
        ///     根据类型创建表结构
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static DataTable CreateTable(Type t)
        {
            return BuiltTable(t.GetProperties());
        }
 
        /// <summary>
        ///     C#.NET数据类型集合
        /// </summary>
        /// <returns></returns>
        public static IList CSharpDataTypes()
        {
            var list = new ArrayList();
            list.Add(typeof(DateTime));
            list.Add(typeof(byte));
            list.Add(typeof(sbyte));
            list.Add(typeof(short));
            list.Add(typeof(int));
            list.Add(typeof(long));
            list.Add(typeof(IntPtr));
            list.Add(typeof(ushort));
            list.Add(typeof(uint));
            list.Add(typeof(ulong));
            list.Add(typeof(UIntPtr));
            list.Add(typeof(float));
            list.Add(typeof(double));
            list.Add(typeof(decimal));
            list.Add(typeof(bool));
            list.Add(typeof(char));
            list.Add(typeof(string));
            return list;
        }
 
        /// <summary>
        ///     数据行(DataRow)转换为对象,对象的Type由type参数决定.
        /// </summary>
        public static object DataRowToObject(DataRow row, Type type)
        {
            if (null == row) return null;
 
            try
            {
                var o = type.Assembly.CreateInstance(type.FullName);
                object v;
                var pinfo = type.GetProperties();
                foreach (var info in pinfo)
                {
                    v = GetFieldValue(row, info.Name);
                    if (v != null) SetPropertyValue(o, info, v);
                }
 
                return o;
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        ///     查找对象包含指定属性.
        /// </summary>
        public static bool FindProperty(object obj, string property)
        {
            try
            {
                if (obj == null) return false;
 
                var type = obj.GetType();
                var pinfo = type.GetProperties();
                foreach (var info in pinfo)
                    if (info.Name.ToUpper() == property.ToUpper())
                        return true;
 
                return false;
            }
            catch
            {
                return false;
            }
        }
 
        /// <summary>
        ///     根据对象的属性取字段的值
        /// </summary>
        private static object GetFieldValue(DataRow row, string propertyName)
        {
            if (row == null) return null;
 
            if (row.Table.Columns.IndexOf(propertyName) >= 0)
            {
                var value = row[propertyName];
                if (value != null && value is DateTime)
                    if ((DateTime)value <= DateTime.MinValue.AddDays(1))
                        value = null;
 
                return value;
            }
 
            return null;
        }
 
        /// <summary>
        ///     纵对象数据组取出某一个对象. 参数指定关键字段名称(keyPropName)及值(keyValue).
        /// </summary>
        public static object GetObjectByKey(IList objects, string keyPropName,
            object keyValue)
        {
            foreach (var o in objects)
            {
                var value = GetValueOfObject(o, keyPropName);
                if (value == null) continue;
 
                if (value.ToStringEx().ToLower() ==
                    keyValue.ToStringEx().ToLower()) return o;
            }
 
            return null;
        }
 
        /// <summary>
        ///     纵对象数据组取出某一个对象. 返回对象指定属性名称(returnPropName)的值
        /// </summary>
        public static object GetObjectValueByKey(IList objects,
            string keyPropName, object keyValue,
            string returnPropName)
        {
            var o = GetObjectByKey(objects, keyPropName, keyValue);
            if (o != null) return GetValueOfObject(o, returnPropName);
 
            return null;
        }
 
        /// <summary>
        ///     获取对象指定属性的值
        /// </summary>
        public static object GetValueOfObject(object obj, string property)
        {
            try
            {
                if (obj == null) return null;
 
                var type = obj.GetType();
                var pinfo = type.GetProperties();
                foreach (var info in pinfo)
                    if (info.Name.ToUpper() == property.ToUpper())
                        return info.GetValue(obj, null);
 
                return null;
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        ///     替换记录对应字段的数据。
        /// </summary>
        /// <param name="sourceRow">数据源</param>
        /// <param name="destRow">需要替换的记录</param>
        public static void ReplaceDataRowChanges(DataRow sourceRow,
            DataRow destRow)
        {
            string fieldName;
 
            //循环处理当前记录的所有字段
            for (var i = 0; i <= sourceRow.Table.Columns.Count - 1; i++)
            {
                fieldName = sourceRow.Table.Columns[i].ColumnName;
 
                //如果字段名相同,替换对应字段的数据。
                if (destRow.Table.Columns.IndexOf(fieldName) >= 0)
                    destRow[fieldName] = sourceRow[fieldName];
            }
        }
 
        /// <summary>
        ///     给资料行赋值
        /// </summary>
        /// <param name="row">资料行</param>
        /// <param name="fieldName">字段名</param>
        /// <param name="value">值</param>
        public static void SetDataRowValue(DataRow row, string fieldName,
            object value)
        {
            row[fieldName] = value == null ||
                             ConvertEx.ToString(value) == Globals.DEF_NULL_VALUE
                ? DBNull.Value
                : value;
        }
 
        /// <summary>
        ///     给对象的属性赋值
        /// </summary>
        /// <param name="instance">对象实例</param>
        /// <param name="prop">属性</param>
        /// <param name="value">值</param>
        public static void SetPropertyValue(object instance, PropertyInfo prop,
            object value)
        {
            try
            {
                if (prop == null) return;
 
                if (prop.PropertyType.ToStringEx() == "System.String")
                {
                }
                else if (prop.PropertyType.ToStringEx() == "System.Decimal")
                {
                    value = decimal.Parse(value.ToStringEx());
                }
                else if (prop.PropertyType.ToStringEx() == "System.Int32")
                {
                    value = int.Parse(value.ToStringEx());
                }
                else if (prop.PropertyType.ToStringEx() == "System.Single")
                {
                    value = float.Parse(value.ToStringEx());
                }
                else if (prop.PropertyType.ToStringEx() == "System.DateTime")
                {
                    value = DateTime.Parse(value.ToStringEx());
                }
 
                prop.SetValue(instance, value, null);
            }
            catch
            {
            }
        }
 
        /// <summary>
        ///     从源行中对相同字段名的列付值
        /// </summary>
        /// <param name="drSouce"></param>
        /// <param name="drTo"></param>
        public static void SetTwoRowSameColValue(DataRow drSource, DataRow drTo)
        {
            for (var i = 0; i < drSource.Table.Columns.Count; i++)
            {
                var fieldname = drSource.Table.Columns[i].ColumnName;
                var col = drTo.Table.Columns[fieldname];
                if (col != null) drTo[fieldname] = drSource[fieldname];
            }
        }
 
        /// <summary>
        ///     给记录的字段赋值
        /// </summary>
        /// <param name="dr">记录</param>
        /// <param name="field">字段名</param>
        /// <param name="value">值</param>
        public static void SetValueofDataRow(DataRow dr, string field,
            object value)
        {
            try
            {
                if (dr == null) return;
 
                dr[field] = value;
            }
            catch
            {
            }
        }
 
        /// <summary>
        ///     设置对象某个属性的值
        /// </summary>
        public static void SetValueOfObject(object obj, string property,
            object value)
        {
            try
            {
                if (obj == null) return;
 
                var type = obj.GetType();
                var pinfo = type.GetProperties();
                foreach (var info in pinfo)
                    if (info.Name.ToUpper() == property.ToUpper())
                    {
                        SetPropertyValue(obj, info, value);
                        break;
                    }
            }
            catch
            {
            }
        }
 
        /// <summary>
        ///     数据库中与C#中的数据类型对照
        /// </summary>
        /// <param name="sqlType"></param>
        /// <returns></returns>
        public static string SqlTypeToCSharpType(string sqlType)
        {
            var reval = string.Empty;
            switch (sqlType.ToLower())
            {
                case "int":
                    reval = "Int32";
                    break;
                case "text":
                    reval = "String";
                    break;
                case "bigint":
                    reval = "Int64";
                    break;
                case "binary":
                    reval = "System.Byte[]";
                    break;
                case "bit":
                    reval = "Boolean";
                    break;
                case "char":
                    reval = "String";
                    break;
                case "datetime":
                    reval = "System.DateTime";
                    break;
                case "decimal":
                    reval = "System.Decimal";
                    break;
                case "float":
                    reval = "System.Double";
                    break;
                case "image":
                    reval = "System.Byte[]";
                    break;
                case "money":
                    reval = "System.Decimal";
                    break;
                case "nchar":
                    reval = "String";
                    break;
                case "ntext":
                    reval = "String";
                    break;
                case "numeric":
                    reval = "System.Decimal";
                    break;
                case "nvarchar":
                    reval = "String";
                    break;
                case "real":
                    reval = "System.Single";
                    break;
                case "smalldatetime":
                    reval = "System.DateTime";
                    break;
                case "smallint":
                    reval = "Int16";
                    break;
                case "smallmoney":
                    reval = "System.Decimal";
                    break;
                case "timestamp":
                    reval = "System.DateTime";
                    break;
                case "tinyint":
                    reval = "System.Byte";
                    break;
                case "uniqueidentifier":
                    reval = "System.Guid";
                    break;
                case "varbinary":
                    reval = "System.Byte[]";
                    break;
                case "varchar":
                    reval = "String";
                    break;
                case "Variant":
                    reval = "Object";
                    break;
                default:
                    reval = "String";
                    break;
            }
 
            return reval;
        }
 
        /// <summary>
        ///     DataTable转换为DataSet
        /// </summary>
        /// <param name="table">DataTable</param>
        /// <returns></returns>
        public static DataSet TableToDataSet(DataTable table)
        {
            if (table.DataSet != null) return table.DataSet;
 
            var ds = new DataSet();
            ds.Tables.Add(table);
            return ds;
        }
 
        /// <summary>
        ///     对象数组转换为ArrayList.
        /// </summary>
        public static ArrayList ToArrayList(IList list)
        {
            if (list == null) return null;
 
            var arrlist = new ArrayList();
            foreach (var o in list) arrlist.Add(o);
 
            return arrlist;
        }
 
        /// <summary>
        ///     对象数组转换为ArrayList.
        /// </summary>
        public static ArrayList ToArrayList(object[] source)
        {
            if (null != source) return new ArrayList(source);
 
            //如果来源数据为null,返回一个空的ArrayList.
            return new ArrayList();
        }
 
        /// <summary>
        ///     ArrayList转换为对象数组.
        /// </summary>
        public static object[] ToObjects(IList source)
        {
            if (null == source) return null;
 
            var ret = new object[source.Count];
            for (var i = 0; i < source.Count; i++) ret[i] = source[i];
 
            return ret;
        }
 
        /// <summary>
        ///     把字符串以逗号分格,转换成数据库格式in('a','b')
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSQLInDataFormat(string input)
        {
            var HQL = string.Empty;
            if (input == string.Empty) return HQL;
 
            var sArray = input.Split(',');
            foreach (var str in sArray)
            {
                if (str.Length == 0) continue;
 
                HQL += "'" + str + "',";
            }
 
            if (HQL.Substring(HQL.Length - 1, 1) == ",")
                HQL = HQL.Substring(0, HQL.Length - 1);
 
            return HQL;
        }
 
        /// <summary>
        ///     把字符串以逗号分格,转换成数据库格式''a'',''b''
        /// </summary>
        public static string ToSQLInDataFormatTwo(string input)
        {
            var HQL = string.Empty;
            if (input == string.Empty) return HQL;
 
            var sArray = input.Split(',');
            foreach (var str in sArray)
            {
                if (str.Length == 0) continue;
 
                HQL += "''" + str + "'',";
            }
 
            if (HQL.Substring(HQL.Length - 1, 1) == ",")
                HQL = HQL.Substring(0, HQL.Length - 1);
 
            return HQL;
        }
 
        /// <summary>
        ///     以逗号分格字符串,返回数组
        ///     如果第一个和最后一个字符为, 去掉
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string[] ToStringSplit(string str)
        {
            if (str.Length > 0)
            {
                if (str[0] == ',') str = str.Substring(1, str.Length - 1);
 
                if (str[str.Length - 1] == ',')
                    str = str.Substring(0, str.Length - 1);
            }
 
            var sArray = str.Split(',');
            return sArray;
        }
 
        /// <summary>
        ///     由某对象更新资料行,字段名称必须与对象属性对应,(ORM模型)
        /// </summary>
        /// <param name="row">资料行</param>
        /// <param name="o">对象实例</param>
        /// <returns></returns>
        public static DataRow UpdateDataRowFromObject(DataRow row, object o)
        {
            var pinfo = o.GetType().GetProperties();
            foreach (var info in pinfo)
                if (row.Table.Columns.IndexOf(info.Name) >= 0)
                    SetDataRowValue(row, info.Name, info.GetValue(o, null));
 
            return row;
        }
 
        /// <summary>
        ///     修改对表的某列的值
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="value"></param>
        public static bool UpdateTableCol(DataTable dt, string fieldName,
            object value)
        {
            try
            {
                if (dt.Columns.IndexOf(fieldName) < 0)
                    throw new Exception("表没有" + fieldName + "列!");
 
                foreach (DataRow dr in dt.Rows) dr[fieldName] = value;
 
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}