有个DataList 要绑定 “学号和姓名”
写在一层的代码如下:string NubAndName;
string Str = "select p_number,p_name from t_people";  //定义查询的SQL语句SqlConnection myConn = new SqlConnection(StringConn);     //建立连接
SqlCommand myComm = new SqlCommand(Str,myConn);myConnection.Open();   // 打开连接并获取数据
try
{
    SqlDataReader myReader = myCommand.ExecuteReader();
    while(myReader.Read())
    { 
        NubAndName = myReader.GetString(0).tostring() + " " + myReader.GetString (1).tostring()
        lsb_item.Items.Add(NubAndName);
    }
    myReader.Close();
    myConn.Close();   // 关闭读取和连接
}
catch(Exception exc)
{
    throw exc;
}可是要写成三层的我就不会了,我想过用数组在“数据访问层”接收,“业务层“不做任何处理,然后在”界面层“通过数组循环给lsb_item 添加值,但是又觉得太复杂,在这里请教大家简单的方法:

解决方案 »

  1.   

    建一个类 有一个方法返回类型为SqlDataReader \   “数据访问层”
    在业务层做处理的吧界面层只是显示的哦
      

  2.   

    DAL層
    StudentDAL.cspublic class StudentDAL
    {
            public static string  SELECT_ALL_STUDENT = "SELECT p_number,p_name FROM t_people";        public static DataTable SelectAllStudent()
            {
                 //將數據庫操作類封裝:如果不封裝那就要重FU的寫數據訪問代碼,你最好可以使用SqlHelper類:此類由微軟提供
                  try
                  {
                  DataSet ds = SqlHelper.ExecuteDataSet(sqlConnectionConnStr , CommandType.Text , SELECT_ALL_STUDENT );
                 return ds.Tables[0];
                   }
                    catch(Exception ex)
                   {
                       //Write log ex;
                        throw new your customer Exception ;
                    }
                 
            }}
    ------------------------------------------------------BLL層
    StudentServices.cspublic class StudentServices
    {
             public ArrayList GetAllNameList()
             {
                      ArrayList al = new ArrayList();
                     DataTable dt = StudentDAL.SelectAllStudent();
                     for(int i=0;i<dt.Rows.Count;i++)
                      { 
                       al.Add(dt.Rows[i][0].ToString() + " " +dt.Rows[i][1].ToString() );
                             
                      }
                 
                      return al;
             }
    }
    ------------------------------------------------------------------
    UI層
    StudentFrm.csprivate void BindStudentName()
    {
          StudentServices studentSrv = new StudentServices();
          ArrayList al = studentSrv .GetAllNameList();
         lsb_item.Item.Clear();
          for(int i = 0 ;i<al.Lenth;i++)
         {
             lsb_item.Items.Add(al[i]);
          }
    }form_load 時就可以call BindStudentName();
      

  3.   

    谢谢,但是我编译的时候提示ArrayList 并不包含对 Lenth 的定义,
    还得请教
      

  4.   

    不好意思寫錯了,是ArrayList 的Count
      

  5.   

    谢谢,看了帮助已经找到答案,用 count 方法。给分了