• ADO.Net属性扩展


    属性扩展

    大体意思:有外键关系时将代号化信息处理成原始文字

    如:Info表中的民族列显示的是民族代号处理成Nation表中的民族名称

          需要在Info类里面扩展一个显示nation名称的属性

    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; }
            }
    
            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();
            }
        }
    }

  • 相关阅读:
    《Maven实战》知识点记录总结
    Git实战
    IDEA下out目录与target目录的区别详解
    Linux下MySQL(8.0.18)的安装
    ElasticSearch学习笔记尚硅谷
    【安装教程】Python IDLE win10+macOS
    【摄影教程笔记】中传《视听语言》
    PyTorch图文安装教程(Win10),含遇到的问题及解决办法
    golangbitmap
    获取IP地址
  • 原文地址:https://www.cnblogs.com/jiuban2391/p/6127335.html
Copyright © 2020-2023  润新知