- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章asp.net DataTable相关操作集锦(筛选,取前N条数据,去重复行,获取指定列数据等)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例总结了asp.net DataTable相关操作。分享给大家供大家参考,具体如下:
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
|
#region DataTable筛选,排序返回符合条件行组成的新DataTable或直接用DefaultView按条件返回
/// <summary>
/// DataTable筛选,排序返回符合条件行组成的新DataTable或直接用DefaultView按条件返回
/// eg:SortExprDataTable(dt,"Sex='男'","Time Desc",1)
/// </summary>
/// <param name="dt">传入的DataTable</param>
/// <param name="strExpr">筛选条件</param>
/// <param name="strSort">排序条件</param>
/// <param name="mode">1,直接用DefaultView按条件返回,效率较高;2,DataTable筛选,排序返回符合条件行组成的新DataTable</param>
public
static
DataTable SortDataTable(DataTable dt,
string
strExpr,
string
strSort,
int
mode)
{
switch
(mode)
{
case
1:
//方法一 直接用DefaultView按条件返回
dt.DefaultView.RowFilter = strExpr;
dt.DefaultView.Sort = strSort;
return
dt;
case
2:
//方法二 DataTable筛选,排序返回符合条件行组成的新DataTable
DataTable dt1 =
new
DataTable();
DataRow[] GetRows = dt.Select(strExpr, strSort);
//复制DataTable dt结构不包含数据
dt1 = dt.Clone();
foreach
(DataRow row
in
GetRows)
{
dt1.Rows.Add(row.ItemArray);
}
return
dt1;
default
:
return
dt;
}
}
#endregion
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#region 获取DataTable前几条数据
/// <summary>
/// 获取DataTable前几条数据
/// </summary>
/// <param name="TopItem">前N条数据</param>
/// <param name="oDT">源DataTable</param>
/// <returns></returns>
public
static
DataTable DtSelectTop(
int
TopItem, DataTable oDT)
{
if
(oDT.Rows.Count < TopItem)
return
oDT;
DataTable NewTable = oDT.Clone();
DataRow[] rows = oDT.Select(
"1=1"
);
for
(
int
i = 0; i < TopItem; i++)
{
NewTable.ImportRow((DataRow)rows[i]);
}
return
NewTable;
}
#endregion
|
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
|
#region 获取DataTable中指定列的数据
/// <summary>
/// 获取DataTable中指定列的数据
/// </summary>
/// <param name="dt">数据源</param>
/// <param name="tableName">新的DataTable的名词</param>
/// <param name="strColumns">指定的列名集合</param>
/// <returns>返回新的DataTable</returns>
public
static
DataTable GetTableColumn(DataTable dt,
string
tableName,
params
string
[] strColumns)
{
DataTable dtn =
new
DataTable();
if
(dt ==
null
)
{
throw
new
ArgumentNullException(
"参数dt不能为null"
);
}
try
{
dtn = dt.DefaultView.ToTable(tableName,
true
, strColumns);
}
catch
(Exception e)
{
throw
new
Exception(e.Message);
}
return
dtn;
}
#endregion
|
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
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Data;
using
System.Collections;
using
System.Text;
namespace
GuanEasy
{
/// <summary>
/// DataSet助手
/// </summary>
public
class
DataSetHelper
{
private
class
FieldInfo
{
public
string
RelationName;
public
string
FieldName;
public
string
FieldAlias;
public
string
Aggregate;
}
private
DataSet ds;
private
ArrayList m_FieldInfo;
private
string
m_FieldList;
private
ArrayList GroupByFieldInfo;
private
string
GroupByFieldList;
public
DataSet DataSet
{
get
{
return
ds; }
}
#region Construction
public
DataSetHelper()
{
ds =
null
;
}
public
DataSetHelper(
ref
DataSet dataSet)
{
ds = dataSet;
}
#endregion
#region Private Methods
private
bool
ColumnEqual(
object
objectA,
object
objectB)
{
if
( objectA == DBNull.Value && objectB == DBNull.Value )
{
return
true
;
}
if
( objectA == DBNull.Value || objectB == DBNull.Value )
{
return
false
;
}
return
( objectA.Equals( objectB ) );
}
private
bool
RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns)
{
bool
result =
true
;
for
(
int
i = 0; i < columns.Count; i++ )
{
result &= ColumnEqual( rowA[ columns[ i ].ColumnName ], rowB[ columns[ i ].ColumnName ] );
}
return
result;
}
private
void
ParseFieldList(
string
fieldList,
bool
allowRelation)
{
if
( m_FieldList == fieldList )
{
return
;
}
m_FieldInfo =
new
ArrayList();
m_FieldList = fieldList;
FieldInfo Field;
string
[] FieldParts;
string
[] Fields = fieldList.Split(
','
);
for
(
int
i = 0; i <= Fields.Length - 1; i++ )
{
Field =
new
FieldInfo();
FieldParts = Fields[ i ].Trim().Split(
' '
);
switch
( FieldParts.Length )
{
case
1:
//to be set at the end of the loop
break
;
case
2:
Field.FieldAlias = FieldParts[ 1 ];
break
;
default
:
return
;
}
FieldParts = FieldParts[ 0 ].Split(
'.'
);
switch
( FieldParts.Length )
{
case
1:
Field.FieldName = FieldParts[ 0 ];
break
;
case
2:
if
( allowRelation ==
false
)
{
return
;
}
Field.RelationName = FieldParts[ 0 ].Trim();
Field.FieldName = FieldParts[ 1 ].Trim();
break
;
default
:
return
;
}
if
( Field.FieldAlias ==
null
)
{
Field.FieldAlias = Field.FieldName;
}
m_FieldInfo.Add( Field );
}
}
private
DataTable CreateTable(
string
tableName, DataTable sourceTable,
string
fieldList)
{
DataTable dt;
if
( fieldList.Trim() ==
""
)
{
dt = sourceTable.Clone();
dt.TableName = tableName;
}
else
{
dt =
new
DataTable( tableName );
ParseFieldList( fieldList,
false
);
DataColumn dc;
foreach
( FieldInfo Field
in
m_FieldInfo )
{
dc = sourceTable.Columns[ Field.FieldName ];
DataColumn column =
new
DataColumn();
column.ColumnName = Field.FieldAlias;
column.DataType = dc.DataType;
column.MaxLength = dc.MaxLength;
column.Expression = dc.Expression;
dt.Columns.Add( column );
}
}
if
( ds !=
null
)
{
ds.Tables.Add( dt );
}
return
dt;
}
private
void
InsertInto(DataTable destTable, DataTable sourceTable,
string
fieldList,
string
rowFilter,
string
sort)
{
ParseFieldList( fieldList,
false
);
DataRow[] rows = sourceTable.Select( rowFilter, sort );
DataRow destRow;
foreach
( DataRow sourceRow
in
rows )
{
destRow = destTable.NewRow();
if
( fieldList ==
""
)
{
foreach
( DataColumn dc
in
destRow.Table.Columns )
{
if
( dc.Expression ==
""
)
{
destRow[ dc ] = sourceRow[ dc.ColumnName ];
}
}
}
else
{
foreach
( FieldInfo field
in
m_FieldInfo )
{
destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];
}
}
destTable.Rows.Add( destRow );
}
}
private
void
ParseGroupByFieldList(
string
FieldList)
{
if
( GroupByFieldList == FieldList )
{
return
;
}
GroupByFieldInfo =
new
ArrayList();
FieldInfo Field;
string
[] FieldParts;
string
[] Fields = FieldList.Split(
','
);
for
(
int
i = 0; i <= Fields.Length - 1; i++ )
{
Field =
new
FieldInfo();
FieldParts = Fields[ i ].Trim().Split(
' '
);
switch
( FieldParts.Length )
{
case
1:
//to be set at the end of the loop
break
;
case
2:
Field.FieldAlias = FieldParts[ 1 ];
break
;
default
:
return
;
}
FieldParts = FieldParts[ 0 ].Split(
'('
);
switch
( FieldParts.Length )
{
case
1:
Field.FieldName = FieldParts[ 0 ];
break
;
case
2:
Field.Aggregate = FieldParts[ 0 ].Trim().ToLower();
Field.FieldName = FieldParts[ 1 ].Trim(
' '
,
')'
);
break
;
default
:
return
;
}
if
( Field.FieldAlias ==
null
)
{
if
( Field.Aggregate ==
null
)
{
Field.FieldAlias = Field.FieldName;
}
else
{
Field.FieldAlias = Field.Aggregate +
"of"
+ Field.FieldName;
}
}
GroupByFieldInfo.Add( Field );
}
GroupByFieldList = FieldList;
}
private
DataTable CreateGroupByTable(
string
tableName, DataTable sourceTable,
string
fieldList)
{
if
( fieldList ==
null
|| fieldList.Length == 0 )
{
return
sourceTable.Clone();
}
else
{
DataTable dt =
new
DataTable( tableName );
ParseGroupByFieldList( fieldList );
foreach
( FieldInfo Field
in
GroupByFieldInfo )
{
DataColumn dc = sourceTable.Columns[ Field.FieldName ];
if
( Field.Aggregate ==
null
)
{
dt.Columns.Add( Field.FieldAlias, dc.DataType, dc.Expression );
}
else
{
dt.Columns.Add( Field.FieldAlias, dc.DataType );
}
}
if
( ds !=
null
)
{
ds.Tables.Add( dt );
}
return
dt;
}
}
private
void
InsertGroupByInto(DataTable destTable, DataTable sourceTable,
string
fieldList,
string
rowFilter,
string
groupBy)
{
if
( fieldList ==
null
|| fieldList.Length == 0 )
{
return
;
}
ParseGroupByFieldList( fieldList );
ParseFieldList( groupBy,
false
);
DataRow[] rows = sourceTable.Select( rowFilter, groupBy );
DataRow lastSourceRow =
null
, destRow =
null
;
bool
sameRow;
int
rowCount = 0;
foreach
( DataRow sourceRow
in
rows )
{
sameRow =
false
;
if
( lastSourceRow !=
null
)
{
sameRow =
true
;
foreach
( FieldInfo Field
in
m_FieldInfo )
{
if
( !ColumnEqual( lastSourceRow[ Field.FieldName ], sourceRow[ Field.FieldName ] ) )
{
sameRow =
false
;
break
;
}
}
if
( !sameRow )
{
destTable.Rows.Add( destRow );
}
}
if
( !sameRow )
{
destRow = destTable.NewRow();
rowCount = 0;
}
rowCount += 1;
foreach
( FieldInfo field
in
GroupByFieldInfo )
{
switch
( field.Aggregate.ToLower() )
{
case
null
:
case
""
:
case
"last"
:
destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];
break
;
case
"first"
:
if
( rowCount == 1 )
{
destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];
}
break
;
case
"count"
:
destRow[ field.FieldAlias ] = rowCount;
break
;
case
"sum"
:
destRow[ field.FieldAlias ] = Add( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );
break
;
case
"max"
:
destRow[ field.FieldAlias ] = Max( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );
break
;
case
"min"
:
if
( rowCount == 1 )
{
destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];
}
else
{
destRow[ field.FieldAlias ] = Min( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );
}
break
;
}
}
lastSourceRow = sourceRow;
}
if
( destRow !=
null
)
{
destTable.Rows.Add( destRow );
}
}
private
object
Min(
object
a,
object
b)
{
if
( ( a
is
DBNull ) || ( b
is
DBNull ) )
{
return
DBNull.Value;
}
if
( ( (IComparable) a ).CompareTo( b ) == -1 )
{
return
a;
}
else
{
return
b;
}
}
private
object
Max(
object
a,
object
b)
{
if
( a
is
DBNull )
{
return
b;
}
if
( b
is
DBNull )
{
return
a;
}
if
( ( (IComparable) a ).CompareTo( b ) == 1 )
{
return
a;
}
else
{
return
b;
}
}
private
object
Add(
object
a,
object
b)
{
if
( a
is
DBNull )
{
return
b;
}
if
( b
is
DBNull )
{
return
a;
}
return
( (
decimal
) a + (
decimal
) b );
}
private
DataTable CreateJoinTable(
string
tableName, DataTable sourceTable,
string
fieldList)
{
if
( fieldList ==
null
)
{
return
sourceTable.Clone();
}
else
{
DataTable dt =
new
DataTable( tableName );
ParseFieldList( fieldList,
true
);
foreach
( FieldInfo field
in
m_FieldInfo )
{
if
( field.RelationName ==
null
)
{
DataColumn dc = sourceTable.Columns[ field.FieldName ];
dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression );
}
else
{
DataColumn dc = sourceTable.ParentRelations[ field.RelationName ].ParentTable.Columns[ field.FieldName ];
dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression );
}
}
if
( ds !=
null
)
{
ds.Tables.Add( dt );
}
return
dt;
}
}
private
void
InsertJoinInto(DataTable destTable, DataTable sourceTable,
string
fieldList,
string
rowFilter,
string
sort)
{
if
( fieldList ==
null
)
{
return
;
}
else
{
ParseFieldList( fieldList,
true
);
DataRow[] Rows = sourceTable.Select( rowFilter, sort );
foreach
( DataRow SourceRow
in
Rows )
{
DataRow DestRow = destTable.NewRow();
foreach
( FieldInfo Field
in
m_FieldInfo )
{
if
( Field.RelationName ==
null
)
{
DestRow[ Field.FieldName ] = SourceRow[ Field.FieldName ];
}
else
{
DataRow ParentRow = SourceRow.GetParentRow( Field.RelationName );
DestRow[ Field.FieldName ] = ParentRow[ Field.FieldName ];
}
}
destTable.Rows.Add( DestRow );
}
}
}
#endregion
#region SelectDistinct / Distinct
/// <summary>
/// 按照fieldName从sourceTable中选择出不重复的行,
/// 相当于select distinct fieldName from sourceTable
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="sourceTable">源DataTable</param>
/// <param name="fieldName">列名</param>
/// <returns>一个新的不含重复行的DataTable,列只包括fieldName指明的列</returns>
public
DataTable SelectDistinct(
string
tableName, DataTable sourceTable,
string
fieldName)
{
DataTable dt =
new
DataTable( tableName );
dt.Columns.Add( fieldName, sourceTable.Columns[ fieldName ].DataType );
object
lastValue =
null
;
foreach
( DataRow dr
in
sourceTable.Select(
""
, fieldName ) )
{
if
( lastValue ==
null
|| !( ColumnEqual( lastValue, dr[ fieldName ] ) ) )
{
lastValue = dr[ fieldName ];
dt.Rows.Add(
new
object
[]{lastValue} );
}
}
if
( ds !=
null
&& !ds.Tables.Contains( tableName ) )
{
ds.Tables.Add( dt );
}
return
dt;
}
/// <summary>
/// 按照fieldName从sourceTable中选择出不重复的行,
/// 相当于select distinct fieldName1,fieldName2,,fieldNamen from sourceTable
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="sourceTable">源DataTable</param>
/// <param name="fieldNames">列名数组</param>
/// <returns>一个新的不含重复行的DataTable,列只包括fieldNames中指明的列</returns>
public
DataTable SelectDistinct(
string
tableName, DataTable sourceTable,
string
[] fieldNames)
{
DataTable dt =
new
DataTable( tableName );
object
[] values =
new
object
[fieldNames.Length];
string
fields =
""
;
for
(
int
i = 0; i < fieldNames.Length; i++ )
{
dt.Columns.Add( fieldNames[ i ], sourceTable.Columns[ fieldNames[ i ] ].DataType );
fields += fieldNames[ i ] +
","
;
}
fields = fields.Remove( fields.Length - 1, 1 );
DataRow lastRow =
null
;
foreach
( DataRow dr
in
sourceTable.Select(
""
, fields ) )
{
if
( lastRow ==
null
|| !( RowEqual( lastRow, dr, dt.Columns ) ) )
{
lastRow = dr;
for
(
int
i = 0; i < fieldNames.Length; i++ )
{
values[ i ] = dr[ fieldNames[ i ] ];
}
dt.Rows.Add( values );
}
}
if
( ds !=
null
&& !ds.Tables.Contains( tableName ) )
{
ds.Tables.Add( dt );
}
return
dt;
}
/// <summary>
/// 按照fieldName从sourceTable中选择出不重复的行,
/// 并且包含sourceTable中所有的列。
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="sourceTable">源表</param>
/// <param name="fieldName">字段</param>
/// <returns>一个新的不含重复行的DataTable</returns>
public
DataTable Distinct(
string
tableName, DataTable sourceTable,
string
fieldName)
{
DataTable dt = sourceTable.Clone();
dt.TableName = tableName;
object
lastValue =
null
;
foreach
( DataRow dr
in
sourceTable.Select(
""
, fieldName ) )
{
if
( lastValue ==
null
|| !( ColumnEqual( lastValue, dr[ fieldName ] ) ) )
{
lastValue = dr[ fieldName ];
dt.Rows.Add( dr.ItemArray );
}
}
if
( ds !=
null
&& !ds.Tables.Contains( tableName ) )
{
ds.Tables.Add( dt );
}
return
dt;
}
/// <summary>
/// 按照fieldNames从sourceTable中选择出不重复的行,
/// 并且包含sourceTable中所有的列。
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="sourceTable">源表</param>
/// <param name="fieldNames">字段</param>
/// <returns>一个新的不含重复行的DataTable</returns>
public
DataTable Distinct(
string
tableName, DataTable sourceTable,
string
[] fieldNames)
{
DataTable dt = sourceTable.Clone();
dt.TableName = tableName;
string
fields =
""
;
for
(
int
i = 0; i < fieldNames.Length; i++ )
{
fields += fieldNames[ i ] +
","
;
}
fields = fields.Remove( fields.Length - 1, 1 );
DataRow lastRow =
null
;
foreach
( DataRow dr
in
sourceTable.Select(
""
, fields ) )
{
if
( lastRow ==
null
|| !( RowEqual( lastRow, dr, dt.Columns ) ) )
{
lastRow = dr;
dt.Rows.Add( dr.ItemArray );
}
}
if
( ds !=
null
&& !ds.Tables.Contains( tableName ) )
{
ds.Tables.Add( dt );
}
return
dt;
}
#endregion
#region Select Table Into
/// <summary>
/// 按sort排序,按rowFilter过滤sourceTable,
/// 复制fieldList中指明的字段的数据到新DataTable,并返回之
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="sourceTable">源表</param>
/// <param name="fieldList">字段列表</param>
/// <param name="rowFilter">过滤条件</param>
/// <param name="sort">排序</param>
/// <returns>新DataTable</returns>
public
DataTable SelectInto(
string
tableName, DataTable sourceTable,
string
fieldList,
string
rowFilter,
string
sort)
{
DataTable dt = CreateTable( tableName, sourceTable, fieldList );
InsertInto( dt, sourceTable, fieldList, rowFilter, sort );
return
dt;
}
#endregion
#region Group By Table
public
DataTable SelectGroupByInto(
string
tableName, DataTable sourceTable,
string
fieldList,
string
rowFilter,
string
groupBy)
{
DataTable dt = CreateGroupByTable( tableName, sourceTable, fieldList );
InsertGroupByInto( dt, sourceTable, fieldList, rowFilter, groupBy );
return
dt;
}
#endregion
#region Join Tables
public
DataTable SelectJoinInto(
string
tableName, DataTable sourceTable,
string
fieldList,
string
rowFilter,
string
sort)
{
DataTable dt = CreateJoinTable( tableName, sourceTable, fieldList );
InsertJoinInto( dt, sourceTable, fieldList, rowFilter, sort );
return
dt;
}
#endregion
#region Create Table
public
DataTable CreateTable(
string
tableName,
string
fieldList)
{
DataTable dt =
new
DataTable( tableName );
DataColumn dc;
string
[] Fields = fieldList.Split(
','
);
string
[] FieldsParts;
string
Expression;
foreach
(
string
Field
in
Fields )
{
FieldsParts = Field.Trim().Split(
" "
.ToCharArray(), 3 );
// allow for spaces in the expression
// add fieldname and datatype
if
( FieldsParts.Length == 2 )
{
dc = dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType(
"System."
+ FieldsParts[ 1 ].Trim(),
true
,
true
) );
dc.AllowDBNull =
true
;
}
else
if
( FieldsParts.Length == 3 )
// add fieldname, datatype, and expression
{
Expression = FieldsParts[ 2 ].Trim();
if
( Expression.ToUpper() ==
"REQUIRED"
)
{
dc = dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType(
"System."
+ FieldsParts[ 1 ].Trim(),
true
,
true
) );
dc.AllowDBNull =
false
;
}
else
{
dc = dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType(
"System."
+ FieldsParts[ 1 ].Trim(),
true
,
true
), Expression );
}
}
else
{
return
null
;
}
}
if
( ds !=
null
)
{
ds.Tables.Add( dt );
}
return
dt;
}
public
DataTable CreateTable(
string
tableName,
string
fieldList,
string
keyFieldList)
{
DataTable dt = CreateTable( tableName, fieldList );
string
[] KeyFields = keyFieldList.Split(
','
);
if
( KeyFields.Length > 0 )
{
DataColumn[] KeyFieldColumns =
new
DataColumn[KeyFields.Length];
int
i;
for
( i = 1; i == KeyFields.Length - 1; ++i )
{
KeyFieldColumns[ i ] = dt.Columns[ KeyFields[ i ].Trim() ];
}
dt.PrimaryKey = KeyFieldColumns;
}
return
dt;
}
#endregion
}
}
|
希望本文所述对大家asp.net程序设计有所帮助.
最后此篇关于asp.net DataTable相关操作集锦(筛选,取前N条数据,去重复行,获取指定列数据等)的文章就讲到这里了,如果你想了解更多关于asp.net DataTable相关操作集锦(筛选,取前N条数据,去重复行,获取指定列数据等)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
前一段时间写过一篇文章《 实战,一个高扩展、可视化低代码前端,详实、完整 》,得到了很多朋友的关注。 其中的逻辑编排部分过于简略,不少朋友希望能写一些关于逻辑编排的内容,本文就详细讲述一下逻辑
我正在尝试以下 Java 片段: int[] testArray={10,20,30,40}; int i= 0; testArray[i++]= testArray[i++]+1; System.o
我想知道我是否可以通过某种方式在 C++ 中进行前/后函数调用。我有一个包含很多函数的包装器类,在每次调用包装器函数后,我应该调用另一个始终相同的函数。 所以我不想像这样对每个函数调用 postFun
我有一个像这样的头文件: #pragma once #include "gamestate.h" #include "ExitListener.h" class InitialGameState :
学习左值和右值。定义是任何可以是“地址”的东西都是左值,否则就是右值。 我检查了运算符的优先级,前缀和后缀增量都比“地址”运算符具有更高的优先级。 对于下面的两个例子,谁能解释一下为什么第一个“&++
在我的学习过程中,我遇到了前后迭代器,我想知道是否有办法让它们就地创建容器元素。从文档来看,容器似乎需要实现 push_back 函数才能与 back_iterator 一起使用。但是有没有一种方法可
我有两个关于 Java 中运算符优先级的类似问题。 第一个: int X = 10; System.out.println(X++ * ++X * X++); //it prints 1440 根据
请放轻松,不要对我开枪,因为我还是新手。 当我运行这段代码时,我完全糊涂了,终生无法弄清楚为什么: int y = 9; cout << "++y = " << ++y << "\n--y = " <
两种表达方式有区别吗: (*x)++ 和 ++(*x) 我可以看到这两个语句都替换了 *x 中 (*x+1) 的内容。但是它们之间有什么区别吗? 最佳答案 (*x)++ 计算为*x的值;作为副作用,*
我有一个如下所示的数据集: Date CONSUMER DISCR CONSUMER STAPLES ENERGY FINANCIALS HEALTH CARE
我希望检查名称字段中输入的前两个字符是否为字母 - 除此之外没有什么区别(空格、'、- 等都是公平的游戏)。这是我到目前为止所拥有的,但它不起作用。想法?谢谢! if (document.form01
我制作了一个简单的脚本,为像素和所有附近的像素着色为相同的颜色 Click foto
我需要编写一个循环,以下列格式输出从昨天算起的最近 30 天: 2014-02-02 2014-02-03 2014-02-04 ... 2014-03-04 我想我需要像这样使用循环: for ($
我正在做一些练习,但我对这个感到困惑: public static int f (int x, int y) { int b=y--; while (b>0) { if (x%2!=0
我需要一个 4 个字符的正则表达式。前 3 个字符必须是数字,最后 1 个字符必须是字母或数字。 我形成了这个,但它不起作用 ^([0-9]{3}+(([a-zA-Z]*)|([0-9]*)))?$
我需要编写一个循环,以下列格式输出从昨天算起的最近 30 天: 2014-02-02 2014-02-03 2014-02-04 ... 2014-03-04 我想我需要像这样使用循环: for ($
我有下面的程序,我试图找到前 1000 个素数的总和。在代码中,解决方案1和2有什么区别?为什么我不应该将 count 变量放在 if 条件之外?如果我把变量放在 if 之外,我显然没有得到我需要的答
这个问题在这里已经有了答案: Replace First N Occurrences in the String (7 个答案) 关闭 4 年前。 我有一个如下的字符串 const str = '_
我正在尝试测量以纳秒为单位的平均访问延迟,但在第一次迭代后我收到“段错误(核心转储)”。我错过了什么吗?我是否滥用了指针。这是导致错误的函数: #include #include #include
我有一个 SQL 问题 (MySQL)。我如何从下表创建一个新表(表名称:“well_master_prod_inj”)。 我需要按井名和日期聚合数据。我希望每个井名只有一行数据以及显示以下数据的列:
我是一名优秀的程序员,十分优秀!