这是一段把DataReader对象转换成DataTable的方法:
    public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
    {
        DataTable datatable = new DataTable();
        DataTable schemaTable = dataReader.GetSchemaTable();
        try
        {
            foreach(DataRow myRow in schemaTable.Rows)
            {
                DataColumn myDataColumn = new DataColumn();
                myDataColumn.DataType = myRow.GetType();
                myDataColumn.ColumnName = myRow[0].ToString();
                datatable.Columns.Add(myDataColumn);            }
            while(dataReader.Read())
            {
                DataRow myDataRow=datatable.NewRow();
                for(int i=0;i<schemaTable.Rows.Count;i++)
                {
                    myDataRow[i] = dataReader[i].ToString();
                }
                datatable.Rows.Add(myDataRow);
                myDataRow = null;
            }
            schemaTable = null;
            dataReader.Close();
            return datatable;
        }
        catch(Exception ex)
        {
            SystemError.SystemLog(ex.Message);
            throw new Exception(ex.Message, ex);
        }
    }
运行时发生以下错误!
myDataRow[i] = dataReader[i].ToString();---->>值类型与列类型不匹配不能在 area_id 列中存储 <hd>。所需类型是 DataRow。
哪为能给排排错啊
先谢谢了!!!!!!!!!

解决方案 »

  1.   

    行记录可不能是String类型的
            ' DataReader 填充到 DataTable 的专用函数!!!
            Public Function C_DataTable(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(CStr(oSchemaTable.Rows(iLoop)("ColumnName")), CType(oSchemaTable.Rows(iLoop)("DataType"), System.Type))
                Next            While oDataReader.Read
                    oDataRow = oDataTable.NewRow                For iLoop = 0 To oSchemaTable.Rows.Count - 1
                        oDataRow(iLoop) = oDataReader(CStr(oSchemaTable.Rows(iLoop)("ColumnName")))
                    Next                oDataTable.Rows.Add(oDataRow)
                End While            oDataReader.Close()
                oSchemaTable.Rows.Clear()            Return oDataTable
            End Function
      

  2.   

    while(dataReader.Read())
                {
                    DataRow myDataRow=datatable.NewRow();
                    for(int i=0;i<schemaTable.Rows.Count;i++)
                    {
                        myDataRow[i] = dataReader[i].ToString();
                    }
                    datatable.Rows.Add(myDataRow);
                    myDataRow = null;
                }这里你也没给出myDataRow的类型啊? myDataRow[i] = dataReader[i].ToString();当然出错啦!老大!
      

  3.   

    设置一下myDataRow的类型就行了!
      

  4.   

    如何设置myDataRow的类型啊
    帮帮忙啊
    感激不尽
      

  5.   

    #Region "转换DataReader 为DataTable"
        Public Function ConvertRederToTable(ByRef reader As SqlDataReader) As DataTable
            Dim iLoop As Integer
            Dim dt As DataTable
            Dim sTable As DataTable
            Dim oDataRow As DataRow        dt = New DataTable
            sTable = New DataTable
            sTable = reader.GetSchemaTable        For iLoop = 0 To sTable.Rows.Count - 1
                dt.Columns.Add(CStr(sTable.Rows(iLoop)("ColumnName")), CType(sTable.Rows(iLoop)("DataType"), System.Type))
            Next
            While reader.Read
                oDataRow = dt.NewRow            For iLoop = 0 To sTable.Rows.Count - 1
                    oDataRow(iLoop) = reader(CStr(sTable.Rows(iLoop)("ColumnName")))
                Next
                dt.Rows.Add(oDataRow)
            End While
            reader.Close()
            sTable.Rows.Clear()
            Return dt
        End Function
    #End Region