使用DataReader填充DataTable
VB.NET
C#
''' -----------------------------------------------------------------------------
''' <summary>
''' Fill DataTable from DataReader
''' </summary>
''' <param name="oDataReader">DataReader对象</param>
''' <returns>返回DataTable</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [Administrator] 2004-9-25 小牛哥 创建
''' </history>
''' -----------------------------------------------------------------------------
Public Function Fill(ByRef oDataReader As IDataReader) As DataTable
Dim iLoop As Integer
Dim oDataTable As DataTable
Dim oSchemaTable As DataTable
Dim oDataRow As DataRow
oDataTable = New DataTable
oSchemaTable = New DataTable
oSchemaTable = oDataReader.GetSchemaTable()
For iLoop = 0 To oSchemaTable.Rows.Count - 1
oDataTable.Columns.Add(oSchemaTable.Rows(iLoop)("ColumnName"), oSchemaTable.Rows(iLoop)("DataType"))
Next
While oDataReader.Read
oDataRow = oDataTable.NewRow
For iLoop = 0 To oSchemaTable.Rows.Count - 1
oDataRow(iLoop) = oDataReader(oSchemaTable.Rows(iLoop)("ColumnName"))
Next
oDataTable.Rows.Add(oDataRow)
End While
oDataReader.Close()
oSchemaTable.Rows.Clear()
Return oDataTable
End Function
''' <summary>
''' Fill DataTable from DataReader
''' </summary>
''' <param name="oDataReader">DataReader对象</param>
''' <returns>返回DataTable</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [Administrator] 2004-9-25 小牛哥 创建
''' </history>
''' -----------------------------------------------------------------------------
Public Function Fill(ByRef oDataReader As IDataReader) As DataTable
Dim iLoop As Integer
Dim oDataTable As DataTable
Dim oSchemaTable As DataTable
Dim oDataRow As DataRow
oDataTable = New DataTable
oSchemaTable = New DataTable
oSchemaTable = oDataReader.GetSchemaTable()
For iLoop = 0 To oSchemaTable.Rows.Count - 1
oDataTable.Columns.Add(oSchemaTable.Rows(iLoop)("ColumnName"), oSchemaTable.Rows(iLoop)("DataType"))
Next
While oDataReader.Read
oDataRow = oDataTable.NewRow
For iLoop = 0 To oSchemaTable.Rows.Count - 1
oDataRow(iLoop) = oDataReader(oSchemaTable.Rows(iLoop)("ColumnName"))
Next
oDataTable.Rows.Add(oDataRow)
End While
oDataReader.Close()
oSchemaTable.Rows.Clear()
Return oDataTable
End Function
C#
public DataTable Fill(ref IDataReader oDataReader)
{
int iLoop;
DataTable oDataTable;
DataTable oSchemaTable;
DataRow oDataRow;
oDataTable = new DataTable();
oSchemaTable = new DataTable();
oSchemaTable = oDataReader.GetSchemaTable();
for (int iLoop = 0; iLoop <= oSchemaTable.Rows.Count - 1; iLoop++) {
oDataTable.Columns.Add(oSchemaTable.Rows(iLoop)("ColumnName"), oSchemaTable.Rows(iLoop)("DataType"));
}
while (oDataReader.Read) {
oDataRow = oDataTable.NewRow;
for (int iLoop = 0; iLoop <= oSchemaTable.Rows.Count - 1; iLoop++) {
oDataRow(iLoop) = oDataReader(oSchemaTable.Rows(iLoop)("ColumnName"));
}
oDataTable.Rows.Add(oDataRow);
}
oDataReader.Close();
oSchemaTable.Rows.Clear();
return oDataTable;
}
{
int iLoop;
DataTable oDataTable;
DataTable oSchemaTable;
DataRow oDataRow;
oDataTable = new DataTable();
oSchemaTable = new DataTable();
oSchemaTable = oDataReader.GetSchemaTable();
for (int iLoop = 0; iLoop <= oSchemaTable.Rows.Count - 1; iLoop++) {
oDataTable.Columns.Add(oSchemaTable.Rows(iLoop)("ColumnName"), oSchemaTable.Rows(iLoop)("DataType"));
}
while (oDataReader.Read) {
oDataRow = oDataTable.NewRow;
for (int iLoop = 0; iLoop <= oSchemaTable.Rows.Count - 1; iLoop++) {
oDataRow(iLoop) = oDataReader(oSchemaTable.Rows(iLoop)("ColumnName"));
}
oDataTable.Rows.Add(oDataRow);
}
oDataReader.Close();
oSchemaTable.Rows.Clear();
return oDataTable;
}