本帖最后由 jiangsheng 于 2011-05-15 23:57:08 编辑

解决方案 »

  1.   

    select 进货单号,经手人,批号,数量 from 表
      

  2.   

    一、连接数据库
    二、查询
    三、接收数据示例代码:
    将那张表封装为实体类,在例子,我用Student类代替public class Student
    {
        public Student(){}   //constructor    public string Name{get;set;}
        public int Age{get;set;}
        public string Hobby{get;set;}
    }
    编写数据访问类StudentService,从数据库查询public class StudentService()
    {  public List<Student> GetAllStudents()
      {     List<Student> list = new List<Student>();     //数据库连接字符串
          string connString = "Data Source =.;Initial Catalog = 数据库;USER ID = sa;PWD = 密码";     //实例化SqlConnection对象
          using(SqlConnection connection = new SqlConnection(connString))
         {
              //SQL查询语句
               string sql = string.Format("select * from student");          //实例化SqlCommand对象
               using(SqlCommand objCommand = new SqlCommand(sql,connection))
             {
                  //利用SqlConnection对象的Open()方法打开数据库连接
                    connection.Open(); 
                  
                  //实例化SqlDataReader对象
                    using(SqlDataReader objReader = objCommand.ExecuteReader())
                  {
                       //利用SqlDataReader的HasRows属性判断是否有数据
                          if(objReader.HasRows)
                       {
                           //利用SqlDataReader的Read()方法循环读取数据
                              while(objReader.Read())
                          {
                               //将数据封装实体类,添加到集合中,返回
                                   Student student = new Student();
                               student.Name = objReader["Name"].ToString();
                               student.Age = Convert.ToInt32(objReader["Age"]);
                               student.Hobby = objReader["Hobby"].ToString();                           list.Add(student);
                          }
                       }
                  }
             }
         }     return list;
      }
    }