C#动态数组(ArrayList )应用可以说在C#开发中是十分常用的,那么具体的实用实例是如何实现的呢?具体的实现步骤和注意事项是什么呢?

下面就是一个C#动态数组实例:用绑定一个DataList的三层代码

C#动态数组之DAL 数据访问层代码:

  1. //绑定IDList,显示所有人员列表  
  2.   public DataSet SelectIDListAll()  
  3.   {  
  4.        string Str = "select p_number,p_name from t_people";   
  5.        DataSet ds = new DataSet();  
  6.  
  7.        myCon = new SqlConnection(DAL.DALConfig.ConnectionString);  
  8.        try 
  9.        {  
  10.         SqlDataAdapter mycomm = new SqlDataAdapter(Str,myCon);  
  11.         mycomm.Fill(ds,"t_people");      
  12.                   
  13.         return ds;  
  14.        }  
  15.        catch(Exception exc)  
  16.        {  
  17.         throw exc;  
  18.        }  
  19.   }  

C#动态数组之BLL业务层代码:

  1. //绑定IDList,显示所有人员列表  
  2.   public ArrayList  SelectIDListAll()  
  3.   {  
  4.        DAL.TPeopleDao peopledao = new TPeopleDao();  
  5.        DataSet ds = new DataSet();  
  6.        ds = peopledao.SelectIDListAll();  
  7.  
  8.        // Creates and initializes a new ArrayList.  
  9.        ArrayList myAL = new ArrayList();  
  10.        for(int i=0;i<ds.Tables[0].Rows.Count;i++)  
  11.        {   
  12.         myAL.Add(ds.Tables[0].Rows[i][0].ToString() +  
  13.  
  14.  " " +ds.Tables[0].Rows[i][1].ToString() );              
  15.        }  
  16.        return myAL;  
  17.   }  

C#动态数组之页面层代码:

  1. //绑定IDList,显示所有人员列表  
  2.   private void SelectIDListAll()  
  3.   {  
  4.        Lab.BLL.TPeopleBiz peoplebiz = new TPeopleBiz();  
  5.        ArrayList myAL = peoplebiz.SelectIDListAll();  
  6.        this.P_IDlist.Items.Clear();  
  7.  
  8.        for(int i = 0 ;i<myAL.Count;i++)  
  9.        {  
  10.         this.P_IDlist.Items.Add(myAL[i]);  
  11.        }  
  12.  
  13.   }  

C#动态数组的应用实例就向你介绍到这里,希望对你了解和学习C#动态数组有所帮助。