使用DataReader填充DataTable

VB.NET
None.gif      ' '' -----------------------------------------------------------------------------
None.gif
     ' '' <summary>
None.gif
     ' '' Fill DataTable from DataReader
None.gif
     ' '' </summary>
None.gif
     ' '' <param name="oDataReader">DataReader对象</param>
None.gif
     ' '' <returns>返回DataTable</returns>
None.gif
     ' '' <remarks>
None.gif
     ' '' </remarks>
None.gif
     ' '' <history>
None.gif
     ' ''     [Administrator]    2004-9-25   小牛哥    创建
None.gif
     ' '' </history>
None.gif
     ' '' -----------------------------------------------------------------------------
ExpandedBlockStart.gifContractedBlock.gif
     Public   Function Fill() Function Fill(ByRef oDataReader As IDataReader) As DataTable
InBlock.gif        
Dim iLoop As Integer
InBlock.gif        
Dim oDataTable As DataTable
InBlock.gif        
Dim oSchemaTable As DataTable
InBlock.gif        
Dim oDataRow As DataRow
InBlock.gif
InBlock.gif        oDataTable 
= New DataTable
InBlock.gif        oSchemaTable 
= New DataTable
InBlock.gif        oSchemaTable 
= oDataReader.GetSchemaTable()
InBlock.gif
InBlock.gif        
For iLoop = 0 To oSchemaTable.Rows.Count - 1
InBlock.gif            oDataTable.Columns.Add(oSchemaTable.Rows(iLoop)(
"ColumnName"), oSchemaTable.Rows(iLoop)("DataType"))
InBlock.gif        
Next
InBlock.gif
InBlock.gif        
While oDataReader.Read
InBlock.gif            oDataRow 
= oDataTable.NewRow
InBlock.gif
InBlock.gif            
For iLoop = 0 To oSchemaTable.Rows.Count - 1
InBlock.gif                oDataRow(iLoop) 
= oDataReader(oSchemaTable.Rows(iLoop)("ColumnName"))
InBlock.gif            
Next
InBlock.gif
InBlock.gif            oDataTable.Rows.Add(oDataRow)
InBlock.gif        
End While
InBlock.gif        oDataReader.Close()
InBlock.gif
InBlock.gif        oSchemaTable.Rows.Clear()
InBlock.gif
InBlock.gif        
Return oDataTable
InBlock.gif
ExpandedBlockEnd.gif    
End Function

C#
None.gif public  DataTable Fill( ref  IDataReader oDataReader) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif 
int iLoop; 
InBlock.gif DataTable oDataTable; 
InBlock.gif DataTable oSchemaTable; 
InBlock.gif DataRow oDataRow; 
InBlock.gif oDataTable 
= new DataTable(); 
InBlock.gif oSchemaTable 
= new DataTable(); 
InBlock.gif oSchemaTable 
= oDataReader.GetSchemaTable(); 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
for (int iLoop = 0; iLoop <= oSchemaTable.Rows.Count - 1; iLoop++dot.gif
InBlock.gif   oDataTable.Columns.Add(oSchemaTable.Rows(iLoop)(
"ColumnName"), oSchemaTable.Rows(iLoop)("DataType")); 
ExpandedSubBlockEnd.gif }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
while (oDataReader.Read) dot.gif
InBlock.gif   oDataRow 
= oDataTable.NewRow; 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
for (int iLoop = 0; iLoop <= oSchemaTable.Rows.Count - 1; iLoop++dot.gif
InBlock.gif     oDataRow(iLoop) 
= oDataReader(oSchemaTable.Rows(iLoop)("ColumnName")); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   oDataTable.Rows.Add(oDataRow); 
ExpandedSubBlockEnd.gif }
 
InBlock.gif oDataReader.Close(); 
InBlock.gif oSchemaTable.Rows.Clear(); 
InBlock.gif 
return oDataTable; 
ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值