屬性擴展
阿新 • • 發佈:2017-07-21
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; }
封裝
屬性擴展