将dataReader转化为DateTable

public static DataTable ConverDateReaderToDataTable(SqlDataReader dataReader)
        {
            DataTable dataTable = new DataTable();
            for (int i = 0; i < dataReader.FieldCount; i++)
            {
                DataColumn myDateColum = new DataColumn();
                myDateColum.DataType = dataReader.GetFieldType(i);
                myDateColum.ColumnName = dataReader.GetName(i);
                dataTable.Columns.Add(myDateColum);
            }
            while (dataReader.Read())
            {
                DataRow myDateRow = dataTable.NewRow();
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    myDateRow[i] = dataReader[i].ToString();
                }
                dataTable.Rows.Add(myDateRow);
                myDateRow = null;
            }
            dataReader.Close();
            return dataTable;
        }

posted on 2008-10-29 14:08  Paul_  阅读(279)  评论(0编辑  收藏  举报

导航