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; }
封装