• 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;
           
            }

  • 相关阅读:
    JVM之GC调优
    JVM的栈、堆
    SpringMVC及其HandlerMapping、HandlerInterceptor、HandlerAdapter等组件的原理解析
    静态代理、动态代理和CGLIB,SpringAOP中的代理
    cmd删除文件夹
    vue-router命名视图+路由嵌套
    Vue实现长按事件
    [SparkSQL] hive.exec.max.dynamic.partitions配置不起作用
    Excel分数转为百分数
    Hadoop YARN主资源调度算法
  • 原文地址:https://www.cnblogs.com/a12110303043/p/5872469.html
Copyright © 2020-2023  润新知