转换DATAREADER为 dataset

C#版

//转换DATAREADER为 dataset
  public static DataTable ConvertDataReaderToDataTable(SqlDataReader reader)
  {
   DataTable objDataTable = new DataTable();

   int intFieldCount = reader.FieldCount ;

   for(int intCounter =0 ;intCounter <= intFieldCount-1; intCounter++)
   {
    objDataTable.Columns.Add(reader.GetName(intCounter),reader.GetFieldType(intCounter));
   }

   //populate datatable
   objDataTable.BeginLoadData();


   //object[] objValues = new object[intFieldCount -1];

    object[] objValues = new object[intFieldCount];

   while (reader.Read())
   {
    reader.GetValues(objValues);

    objDataTable.LoadDataRow(objValues,true);
   }

   reader.Close();

   objDataTable.EndLoadData();

   return objDataTable;


  }

VB

 ' convert datareader to dataset
        Public Function ConvertDataReaderToDataTable(ByVal reader As IDataReader) As DataTable

            ' create datatable from datareader
            Dim objDataTable As New DataTable
            Dim intFieldCount As Integer = reader.FieldCount
            Dim intCounter As Integer
            For intCounter = 0 To intFieldCount - 1
                objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter))
            Next intCounter

            ' populate datatable
            objDataTable.BeginLoadData()
            Dim objValues(intFieldCount - 1) As Object
            While reader.Read()
                reader.GetValues(objValues)
                objDataTable.LoadDataRow(objValues, True)
            End While
            reader.Close()
            objDataTable.EndLoadData()

            Return objDataTable

        End Function

posted @ 2006-05-29 09:56  dodo-yufan  阅读(2545)  评论(0编辑  收藏  举报