0914 c#的遍历查询-属性扩展

c#的遍历查询

昨天我们的查询只能查询一条数据,查询的只是学号是输入的学号的那个同学的信息,今天我们看一下查看所有的信息

其实步骤跟昨天一样,只需要把查询出来的每个对象放入集合中,在program里面挨个输出来就行了

这是我们的函数,返回值返回一个泛型集合,数据类型就是我们对象的类型,没有长度.

public List<xuesheng> select1()
        {                         
            List<xuesheng> list = new List<xuesheng>();//实例化集合
            cmd.CommandText = "select *from xuesheng";//开始查询
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)//有数据就进
            {
                while (dr.Read())//将数据库里面的数据放到对象里面
                {
                    xuesheng xs = new xuesheng();
                    xs.Code = int.Parse(dr["code"].ToString());
                    xs.Name = dr["name"].ToString();
                    xs.Sex = dr["sex"].ToString();
                    xs.Banji = dr["banji"].ToString();
                    xs.Birthday = DateTime.Parse(dr["birthday"].ToString());
                    xs.Nation = dr["nation"].ToString();

                    list.Add(xs);//将对象挨个放入集合里面
                }

            }
            else
            {
                Console.WriteLine("数据仓库中没有信息!");
            }
            conn.Close();
            return list;//返回集合
        }

剩下的只要在program里面遍历集合就行了

 List<xuesheng> list = new List<xuesheng>();//实例化集合
                    xueshengsj xss = new xueshengsj();
                    list = xss.select1();
                    foreach (xuesheng xs in list)
                    {
                        Console.WriteLine((xs.Code.ToString()) + "   " + xs.Name + "   " + xs.Sex + "   " + xs.Banji + "   " + xs.BirthdayStr + "   " + xs.nation);

                    }

 

属性扩展

简单的属性扩展在实体类里面直接写就行

 private DateTime _birthday;
        public DateTime Birthday
        {
            get { return _birthday; }
            set { _birthday = value; }
        }
        public string BirthdayStr//属性扩展
        {
            get { return _birthday.ToString("yyyy年MM月dd日"); }
        }

用到函数的就需要重新写数据操作类

private string _nation;
        public string Nation
        {
            get { return _nation; }
            set { _nation = value; }
        }
        public string nation//属性扩展,下面写出扩展的nationlgsj的函数
        {

            get
            {
                nationlgsj nsj = new nationlgsj();
                string language = nsj.select(_nation);
                return language;
            }
        }

 

 

 

上面属性扩展的函数

public string select(string nation)
        {
            string language = null;
            cmd.CommandText = "select *from minzu where Nation=@nation";//查询的是另一个表
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@nation",nation);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if(dr.HasRows)
            {
                dr.Read();
                language = dr["language"].ToString();//将民族对应的语言写出来
            }
            conn.Close();

 

            return language;
       
        }

posted @ 2016-09-14 15:36  王站住  阅读(182)  评论(0编辑  收藏  举报