首页 > 代码库 > ADO.Net属性扩展
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(); } } }
ADO.Net属性扩展
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。