将一个DataTable转换成列表

---------------------------------方法名------------------------------------------------------------------------------------------------------

 /// <summary>
        /// 将一个DataTable转换成列表
        /// </summary>
        /// <typeparam name="T">实体对象的类型</typeparam>
        /// <param name="dt">要转换的DataTable</param>
        /// <returns>static</returns>
        public  List<T> DataTableToEntityList<T>(DataTable dt)
        {
            List<T> entiyList = new List<T>();

            Type entityType = typeof(T);
            PropertyInfo[] entityProperties = entityType.GetProperties();

            foreach (DataRow row in dt.Rows)
            {
                T entity = Activator.CreateInstance<T>();

                foreach (PropertyInfo propInfo in entityProperties)
                {
                    if (dt.Columns.Contains(propInfo.Name))
                    {
                        if (!row.IsNull(propInfo.Name))
                        {
                            propInfo.SetValue(entity, row[propInfo.Name], null);
                        }
                    }
                }

                entiyList.Add(entity);
            }

            return entiyList;
        }

--------------------------实现Datable 作为填充参数---------------------------------------------------------------------------------

 public DataTable GetTable(string strSql)
        {
            SqlConnection con = new SqlConnection("Data Source=ZYSOFT;Initial Catalog= Work; Integrated Security = True");
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            //if (sqlConn.State == ConnectionState.Closed && sqlConn.State == ConnectionState.Closed)        //若原来的状态为关闭且当前连接未打开
            //{
            //    sqlConn.Open();
            //}
            SqlCommand cmd = new SqlCommand(strSql, con);
            DataTable data = new DataTable();
            SqlDataReader reader = cmd.ExecuteReader();
            data.Load(reader);
            return data;
        }

------------------------------------构建 一个实体类---------------------------------------------------

 public class Receipt
    {
        private int taskID;

        public int TaskID
        {
            get { return taskID; }
            set { taskID = value; }
        }

        private string receiptID;

        public string ReceiptID
        {
            get { return receiptID; }
            set { receiptID = value; }
        }

        private string receiptName;

        public string ReceiptName
        {
            get { return receiptName; }
            set { receiptName = value; }
        }
    -----------------------------------------在webservice 中返回------------------------------------------------------

 [WebMethod]
        public List<Receipt> GetDataTableToEntityList()
        {
         
                DataService s = new DataService();
                SqlHelper helper = new SqlHelper();
                DataTable dt = helper.GetTable(" select * from dbo.Receipt");
                return s.DataTableToEntityList<Receipt>(dt);
        }

---------------------------------------------------在前台页面调用 -------------------------------------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetData();
            }
        }

        public void GetData()
        {
            localhost.Service1 s = new WebApplication1.localhost.Service1();
            s.GetDataTableToEntityList();
            for (int i = 0; i < 10; i++)
            {
                this.ListBox1.Items.Add(s.GetDataTableToEntityList()[i].ReceiptID + s.GetDataTableToEntityList()[i].TaskID + s.GetDataTableToEntityList()[i].ReceiptName);

            }
        }


    }
}

原文地址:https://www.cnblogs.com/dullbaby/p/2949924.html