日期:2014-05-20  浏览次数:20988 次

如何从数据库中提取数据添加到combox控件中
在数据访问层中写个方法接受从数据库中查询出来的班级
 public string GetAllGrade(string sql)
 {
  SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings ["conn1"].ConnectionString);
  SqlCommand cmd = new SqlCommand(sql, conn);
  SqlDataReader reader=cmd.ExecuteReader();
  string gradeName="";
  while(reader.Read())
  {
  gradeName=Convert.ToString(reader["GradeName"]);
  }
  return gradeName;  
 }
在业务逻辑层输入sql语句
 public string GetAllGrade()
 {
  StudentDAL studentDal=new StudentDAL();
  string sql="select gradeName from Grade";
  return studentDal.GetAllGrade(sql);
 }
在表示层的窗体Load事件中进行显示
 StudentBLL studentBll=new StudentBLL();
 string gradeName=studentBll.GetAllGrade();
 cboGrade.Items.Add(gradeName);

我遇到的问题是后面数据把前面数据覆盖了,请问一下这个事怎么回事啊???

------解决方案--------------------
public string GetAllGrade(string sql) 

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings ["conn1"].ConnectionString); 
SqlCommand cmd = new SqlCommand(sql, conn); 
SqlDataReader reader=cmd.ExecuteReader(); 
string gradeName=""; 
while(reader.Read()) 

gradeName [color=#FFFF00]+= Convert.ToString(reader["GradeName"]); [/color]

return gradeName;

在业务逻辑层输入sql语句 
public string GetAllGrade() 

StudentDAL studentDal=new StudentDAL(); 
string sql="select gradeName from Grade"; 
return studentDal.GetAllGrade(sql); 

在表示层的窗体Load事件中进行显示 
StudentBLL studentBll=new StudentBLL(); 
string gradeName=studentBll.GetAllGrade(); 
cboGrade.Items.Add(gradeName); 

------解决方案--------------------
gradeName += Convert.ToString(reader["GradeName"]);