1. 程式人生 > >屬性擴展

屬性擴展

ext user nat lec .com rom 擴展 屬性 public

   private bool _Sex;

        public bool Sex
        {
            get { return _Sex; }
            set { _Sex = value; }
        }

        public string SexStr
        {
            get { return _Sex ? "" : ""; }
        }


        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 NationName { get { return new UserNationData().SelectNation(_Nation); } }

方法

public class UserNationData
    {
        SqlConnection conn;
        SqlCommand cmd;

        
public UserNationData() { conn = new SqlConnection("server=.;database=Date0504;user=sa;pwd=y920106k"); cmd = conn.CreateCommand(); } public string SelectNation(string nc) { string s = "無數據"; cmd.CommandText = "select *from UserNation where NationCode = @a"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a", nc); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); s = dr["NationName"].ToString(); } conn.Close(); return s; }

封裝

屬性擴展