首页 > 代码库 > 实体类、数据访问类中的属性拓展

实体类、数据访问类中的属性拓展

类中:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace 实体类_数据访问类.App_Code
{
    public class Users
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;

        public Users()
        {
            conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=123");
            cmd = conn.CreateCommand();
        }


        private int _Ids;

        /// <summary>
        /// ids
        /// </summary>
        public int Ids
        {
            get { return _Ids; }
            set { _Ids = value; }
        }
        private string _UserName;

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }
        private string _PassWord;

        /// <summary>
        /// 密码
        /// </summary>
        public string PassWord
        {
            get { return _PassWord; }
            set { _PassWord = value; }
        }
        private string _NickName;

        /// <summary>
        /// 昵称
        /// </summary>
        public string NickName
        {
            get { return _NickName; }
            set { _NickName = value; }
        }

        public string NickAndUser
        {
            get
            {
                return _NickName + "(" + _UserName + ")";
            }
        }


        private bool _Sex;

        /// <summary>
        /// 性别
        /// </summary>
        public bool Sex
        {
            get { return _Sex; }
            set { _Sex = value; }
        }
        //判断是男是女
        public string SexStr
        {
            get
            {
                return _Sex ? "男" : "女";
            }
        }


        private DateTime _Birthday;

        /// <summary>
        /// 生日
        /// </summary>
        public DateTime Birthday
        {
            get { return _Birthday; }
            set { _Birthday = value; }
        }
        //将datetime类型转换为string类型
        public string BirthdayStr
        {
            get
            {
                return _Birthday.ToString("yyyy年MM月dd日");
            }
        }


        private string _Nation;

        /// <summary>
        /// 民族
        /// </summary>
        public string Nation
        {
            get { return _Nation; }
            set { _Nation = value; }
        }
        //将民族用字符串显示出来
        public string NationName
        {
            get
            {
                string end = "<未填>";
                cmd.CommandText = "select *from Nation where NationCode = @a";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@a",_Nation);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    end=dr["NationName"].ToString();
                }
                conn.Close();

                return end;
            }
        }
        private string _Class;

        /// <summary>
        /// 班级
        /// </summary>
        public string Class
        {
            get { return _Class; }
            set { _Class = value; }
        }
        //将班级用字符串展示出来
        public string ClassName
        {
            get
            {
                string end = "<无班级>";

                cmd.CommandText = "select *from Class where ClassCode = @a";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@a",_Class);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    end = dr["ClassName"].ToString();
                }
                conn.Close();
                return end;
            }
        }
    }
}

主程序中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 实体类_数据访问类.App_Code;

namespace 属性扩展
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Users> ulist = new UsersData().Select();

            Console.WriteLine("序号   密码   昵称   性别   生日   民族   班级");
            foreach (var u in ulist)
            {
                Console.WriteLine(u.Ids + "   " + u.PassWord + "   " + u.NickAndUser + "   " + u.SexStr + "   " + u.BirthdayStr + "   " + u.NationName + "   " + u.ClassName);
            }
            Console.ReadKey();
        }
    }
}

技术分享

实体类、数据访问类中的属性拓展