设计行政区划程序的步骤
Author:水如烟
总目录:行政区划数据方案设计
上一篇,行政区划程序的设计(十三)
在上面的测试中,我感觉某些类还是混乱。为此,有必要先理顺一下。
混乱的起源在于处理几个表的集合。为此,我在LzmTW上建一个泛型类,专门处理这种表集合。
类DataTableCollection,三个文件:
DataTableCollection.vb
NamespaceuSystem.uData
PublicClassDataTableCollection(OfTAsDataTable)
PrivategDataSetAsNewDataSet
PrivategTableNameListAsNewList(OfString)
'引入只读集合,不对外公开gDataSet和gTableNameList
PublicReadOnlyPropertyDataTables()AsSystem.Collections.ObjectModel.ReadOnlyCollection(OfT)
Get
DimmTableListAsNewList(OfT)
ForEachtableAsDataTableIngDataSet.Tables
mTableList.Add(CType(table,T))
Next
ReturnNewSystem.Collections.ObjectModel.ReadOnlyCollection(OfT)(mTableList)
EndGet
EndProperty
PublicReadOnlyPropertyTableNames()AsSystem.Collections.ObjectModel.ReadOnlyCollection(OfString)
Get
ReturnNewSystem.Collections.ObjectModel.ReadOnlyCollection(OfString)(gTableNameList)
EndGet
EndProperty
PublicReadOnlyPropertyIsEmpty()AsBoolean
Get
Return(gTableNameList.Count=0)
EndGet
EndProperty
PublicReadOnlyPropertyItem(ByValtableNameAsString)AsT
Get
IfNotContains(tableName)ThenReturnNothing
ReturnCType(gDataSet.Tables(tableName),T)
EndGet
EndProperty
PublicFunctionContains(ByValtableNameAsString)AsBoolean
ReturngTableNameList.Contains(tableName)
EndFunction
'''<summary>
'''DataTable的相互转换
'''</summary>
'''<typeparamname="S">源表类型</typeparam>
'''<typeparamname="D">目标表类型</typeparam>
'''<paramname="table">源表</param>
'''<remarks>返回目标表的新实例并保留源表的表名</remarks>
PublicSharedFunctionConvert(OfSAsDataTable,DAsDataTable)(ByValtableAsS)AsD
DimmResultAsD
mResult=CType(System.Activator.CreateInstance(GetType(D)),D)
mResult.TableName=table.TableName'保留源表的表名
'公共的列名,不考虑列的类型是否相同
DimmCommonColumns(-1)AsString
ForEachcolumnAsDataColumnIntable.Columns
IfmResult.Columns.Contains(column.ColumnName)Then
uCollections.CommonServices.Append(OfString)(mCommonColumns,column.ColumnName)
EndIf
Next
'如果没有公共的列,则返回一个空表
IfmCommonColumns.Length=0ThenReturnmResult
'不处理键值,如不符,会发生异常
DimmResultRowAsDataRow
ForEachrowAsDataRowIntable.Rows
mResultRow=mResult.NewRow
ForEachcolumnAsStringInmCommonColumns
mResultRow(column)=row(column)
Next
mResult.Rows.Add(mResultRow)
Next
mResult.AcceptChanges()
ReturnmResult
EndFunction
EndClass
EndNamespace
PublicClassDataTableCollection(OfTAsDataTable)
PrivategDataSetAsNewDataSet
PrivategTableNameListAsNewList(OfString)
'引入只读集合,不对外公开gDataSet和gTableNameList
PublicReadOnlyPropertyDataTables()AsSystem.Collections.ObjectModel.ReadOnlyCollection(OfT)
Get
DimmTableListAsNewList(OfT)
ForEachtableAsDataTableIngDataSet.Tables
mTableList.Add(CType(table,T))
Next
ReturnNewSystem.Collections.ObjectModel.ReadOnlyCollection(OfT)(mTableList)
EndGet
EndProperty
PublicReadOnlyPropertyTableNames()AsSystem.Collections.ObjectModel.ReadOnlyCollection(OfString)
Get
ReturnNewSystem.Collections.ObjectModel.ReadOnlyCollection(OfString)(gTableNameList)
EndGet
EndProperty
PublicReadOnlyPropertyIsEmpty()AsBoolean
Get
Return(gTableNameList.Count=0)
EndGet
EndProperty
PublicReadOnlyPropertyItem(ByValtableNameAsString)AsT
Get
IfNotContains(tableName)ThenReturnNothing
ReturnCType(gDataSet.Tables(tableName),T)
EndGet
EndProperty
PublicFunctionContains(ByValtableNameAsString)AsBoolean
ReturngTableNameList.Contains(tableName)
EndFunction
'''<summary>
'''DataTable的相互转换
'''</summary>
'''<typeparamname="S">源表类型</typeparam>
'''<typeparamname="D">目标表类型</typeparam>
'''<paramname="table">源表</param>
'''<remarks>返回目标表的新实例并保留源表的表名</remarks>
PublicSharedFunctionConvert(OfSAsDataTable,DAsDataTable)(ByValtableAsS)AsD
DimmResultAsD
mResult=CType(System.Activator.CreateInstance(GetType(D)),D)
mResult.TableName=table.TableName'保留源表的表名
'公共的列名,不考虑列的类型是否相同
DimmCommonColumns(-1)AsString
ForEachcolumnAsDataColumnIntable.Columns
IfmResult.Columns.Contains(column.ColumnName)Then
uCollections.CommonServices.Append(OfString)(mCommonColumns,column.ColumnName)
EndIf
Next
'如果没有公共的列,则返回一个空表
IfmCommonColumns.Length=0ThenReturnmResult
'不处理键值,如不符,会发生异常
DimmResultRowAsDataRow
ForEachrowAsDataRowIntable.Rows
mResultRow=mResult.NewRow
ForEachcolumnAsStringInmCommonColumns
mResultRow(column)=row(column)
Next
mResult.Rows.Add(mResultRow)
Next
mResult.AcceptChanges()
ReturnmResult
EndFunction
EndClass
EndNamespace
本文地址:http://www.45fan.com/dnjc/66953.html