关于集合类的做法示例实体类赋值cnblogs - 技术追寻- 博客园
 

关于集合类的做法!

类工程:

using System;

using System.Data;

namespace MyProduct.Employee

{

 /// <summary>

 /// Employee的实体类

 /// </summary>

 public class EmployeeEntity

 {

        private int mEmployeeID;

        private string mLastName;

        private string mFirstName;

        public int EmployeeID

        {

            get { return mEmployeeID; }

            set { mEmployeeID = value; }

        }

        public string LastName

        {

            get { return mLastName; }

            set { mLastName = value; }

        }

        public string FirstName

        {

            get { return mFirstName; }

            set { mFirstName = value; }

        }

  public EmployeeEntity()

  {

  

  }

        /// <summary>

        /// 构造函数,给实体类赋值

        /// </summary>

        /// <param name="pDR">DataRow</param>

        public EmployeeEntity(DataRow pDR)

        {

            Fill(pDR);

        }

        /// <summary>

        /// 给实体类赋值

        /// </summary>

        /// <param name="pDR">DataRow</param>

        public void Fill(DataRow pDR)

        {           

            foreach( System.Reflection.PropertyInfo pi in this.GetType().GetProperties())

            {

                if (pi.CanWrite)

                {

                    try

                    {

                        if (pDR[ pi.Name ] != null && !pDR.IsNull( pi.Name ) )

                        {     

                            pi.Setvalue(this, pDR[ pi.Name ], null);

                        }

                        else

                        {

                            pi.Setvalue(this, null, null);

                        }

                    }

                    catch

                    {

                        pi.Setvalue(this, null, null);

                    }

                }

            }

  

        }

 }

/// <summary>

    /// Employee的集合类

    /// </summary>

    public class EmployeeList : System.Collections.CollectionBase

    {

        public EmployeeList()

        {

  

        }

        /// <summary>

        /// 构造函数,给集合类赋值

        /// </summary>

        /// <param name="pDT">DataTable</param>

        public EmployeeList(DataTable pDT)

        {

            Fill(pDT);

        }

        /// <summary>

        /// 给集合类赋值

        /// </summary>

        /// <param name="pDT">DataTable</param>

        public void Fill(DataTable pDT)

        {

            foreach(DataRow pDR in pDT.Rows)

            {

                this.InnerList.Add( new EmployeeEntity( pDR ) );

            }

        }

        /// <summary>

        /// 索引

        /// </summary>

        public virtual EmployeeEntity this[int Index]

        {

            get

            {

                return (EmployeeEntity)this.List[Index];

            }

        }

        /// <summary>

        /// 添加

        /// </summary>

        /// <param name="pEmp">实体类</param>

        public virtual void Add(EmployeeEntity pEmp)

        {

            this.List.Add(pEmp);

        }

        /// <summary>

        /// 删除

        /// </summary>

        /// <param name="Index">index</param>

        public virtual void Remove(int Index)

        {

            this.List.RemoveAt(Index);

        }

    }

    /// <summary>

    /// Employee业务类

    /// </summary>

    public class EmployeeRule

    {

        /// <summary>

        /// 获取所有

        /// </summary>

        /// <returns></returns>

        public EmployeeList GetAllEmployees()

        {

            string sql;

            sql="select * from employees";

            DataTable pDT=ExeForDtl(sql);

            EmployeeList emplist=new EmployeeList();           

            emplist.Fill(pDT);

            return emplist;

        }

        /// <summary>

        /// 获取一个

        /// </summary>

        /// <param name="pEmployeeID"></param>

        /// <returns></returns>

public EmployeeEntity GetOneEmployee(int pEmployeeID)

        {

            string sql;

            sql="select * from employees where employeeid="+pEmployeeID;

          

            DataTable pDT=new DataTable();

            pDT=ExeForDtl(sql);

            if(pDT.Rows.Count>0)

            {

                return new EmployeeEntity(pDT.Rows[0]);

            }

            else

            {

                return null;

            }

        }

    }

}

Web工程:

这里的Web工程说明了怎样去调用我们写的集合类。

1。在DataGrid中使用集合类

直接绑定:

EmployeeRule empRule=new EmployeeRule();

DataGrid1.DataSource=empRule.GetAllEmployees();

DataGrid1.DataBind();

手工添加绑定:

EmployeeEntity emp=new EmployeeEntity();

emp.EmployeeID=10;

emp.FirstName="mike";

emp.LastName="daoson";

EmployeeList empList=new EmployeeList();

empList.Add(emp);

DataGrid1.DataSource=empList;

DataGrid1.DataBind();

2。给文本框赋值

EmployeeEntity emp=new EmployeeEntity();

emp=empRule.GetOneEmployee(1);

txtEmployeeID.Text=emp.EmployeeID.ToString();

txtFirstName.Text=emp.FirstName;

txtLastName.Text=emp.LastName;

下面讲一下此种方法的优缺点:

优点:

1。通过反射,循环给属性赋值,省去了给属性赋值的大量工作量

2。集合类可以直接绑定到DataGrid中

3。自己如果想构造一个虚拟的DataTable,可以通过集合类来做

4。这种方法可以用foreach做循环来检索出他的实体类

5。这里可以直接把datarow赋给实体类,也可以给实体类手工赋值

缺点:

1。数据库中的字段名必须要和实体类中的属性相一致

2。如果是多个表关联,那么集合类就不能绑定到DataGrid

 


郑重声明:资讯 【关于集合类的做法示例实体类赋值cnblogs - 技术追寻- 博客园】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——