using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// ConvertReaderToTable 的摘要说明
/// </summary>
namespace Tools
{
    public class ConvertReaderToTable
    {
        public ConvertReaderToTable()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public static DataTable Convertdrtodt(SqlDataReader dr)
        {
            DataTable dataTable = new DataTable();//建一个新的实例


            for (int i = 0; i < dr.FieldCount; i++)
            {
                DataColumn mydc = new DataColumn();//关键的一步
                mydc.DataType = dr.GetFieldType(i);
                mydc.ColumnName = dr.GetName(i);

                dataTable.Columns.Add(mydc);//关键的第二步
            }

            while (dr.Read())
            {
                DataRow mydr = dataTable.NewRow();//关键的第三步
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    mydr[i] = dr[i].ToString();
                }

                dataTable.Rows.Add(mydr);//关键的第四步
                mydr = null;
            }

            dr.Close();
            return (dataTable);//别忘了要返回datatable，否则出错

        }
    }
}