实体类数据访问类属性扩展
Posted 疯丫头0805
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实体类数据访问类属性扩展相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 实体类_数据访问类.App_Code { public class Users { 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; } } private bool _sex; /// <summary> /// 性别 /// </summary> public bool sex { get { return _sex; } set { _sex = value; } } //属性扩展,可以再添加一个方法,把性别转换成字符串类型,打印时直接u.sex1,不用再在program里面每次单独转换 public string sex1 { get { return (_sex ? "男" : "女"); }//只读取,不能填写或改写 } private DateTime _brithday; /// <summary> /// 生日 /// </summary> public DateTime brithday { get { return _brithday; } set { _brithday = value; } } //属性扩展 public string brithday1 { get { return _brithday.ToString("yyyy年MM月dd日");} } private string _nation; /// <summary> /// 民族 /// </summary> public string nation { get { return _nation; } set { _nation = value; } } //属性扩展 public string nationname { get { NationData ndata = new NationData(); string end = ndata.select(_nation); return end; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace 实体类_数据访问类.App_Code { public class UsersData { //调用数据连接类、数据操作类 SqlConnection conn = null;//等于null,不占空间 SqlCommand cmd = null; //构造函数 public UsersData()//构造函数名称与类名一致 { conn = new SqlConnection("server=.;database=Data0720;user=sa;pwd=123;"); cmd = conn.CreateCommand(); } //添加 /// <summary> /// Users表添加方法 /// </summary> /// <param name="u">要添加到数据库中的Users对象</param> public void Insert(Users u)//有实体类,括号内的参数把实体类的对象放进去 { cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f)"; cmd.Parameters.Clear();//一定要先清空 cmd.Parameters.Add("@a", u.username); cmd.Parameters.Add("@b", u.password); cmd.Parameters.Add("@c", u.nickname); cmd.Parameters.Add("@d", u.sex); cmd.Parameters.Add("@e", u.brithday); cmd.Parameters.Add("@f", u.nation); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } //查询(一条信息) /// <summary> /// Users表查询方法 /// </summary> /// <param name="Uname">查询数据库中的Users信息表</param> /// <returns></returns> public Users select(string Uname) { Users u = null; //查 cmd.CommandText = "select * from Users where [email protected]"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a",Uname); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); u = new Users(); u.username = dr["username"].ToString(); u.password = dr["password"].ToString(); u.nickname = dr["nickname"].ToString(); u.sex = Convert.ToBoolean(dr["sex"]); u.brithday = Convert.ToDateTime(dr["brithday"]); u.nation = dr["nation"].ToString(); } conn.Close(); return u; } //删除 /// <summary> /// Users表删除方法 /// </summary> /// <param name="Uname">删除数据库中的Users信息表中的信息</param> public bool Delete(string Uname) { bool delok = false; cmd.CommandText = "delete from Users where [email protected]"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a",Uname); conn.Open(); try//验证、判断是否删除成功,有可能是数据库服务没开,服务器连接不上,这样不可以返回删除成功 { cmd.ExecuteNonQuery();//如果没执行成功,程序报错,用try包起来 delok = true; } catch { }//让catch捕获住错误,不让程序崩溃,执行delok = false;,这个是一开始设置的默认的,所以catch可以空着 //finally//必须走, //{ conn.Close();//这一步必须走,finally可写可不写 //} return delok; } //修改 /// <summary> /// Users表修改方法 /// </summary> /// <param name="u">修改数据库中的Users信息表中的信息</param> public void Update(Users u3) { cmd.CommandText = "update Users set [password][email protected],[email protected] where username [email protected]"; cmd.Parameters.Clear(); cmd.Parameters.Add("@b",u3.password); cmd.Parameters.Add("@d",((u3.sex==true)?"1":"0")); cmd.Parameters.Add("@a",u3.username); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } //查询所有信息 public List<Users> select()//用泛型集合,不规定个数,单数固定类型都得是Users类型,放置所有Users数据对象 { List<Users> list = new List<Users>(); cmd.CommandText = "select * from Users"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)//如果数据表中有数据 { while (dr.Read())//循环读取全部数据 { //每读取一行数据,就只做一个Users对象 Users u = new Users(); u.username = dr["username"].ToString(); u.password = dr["password"].ToString(); u.nickname = dr["nickname"].ToString(); u.sex = Convert.ToBoolean(dr["sex"]); u.brithday = Convert.ToDateTime(dr["brithday"]); u.nation = dr["nation"].ToString(); //在循环中,把制作好的每一个对象都放到集合中去 list.Add(u); } } conn.Close(); return list; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 实体类_数据访问类.App_Code { public class Nation { private string _nationcode; public string nationcode { get { return _nationcode; } set { _nationcode = value; } } private string _nationname; public string nationname { get { return _nationname; } set { _nationname = value; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace 实体类_数据访问类.App_Code { public class NationData { SqlConnection conn = null; SqlCommand cmd = null; public NationData() { conn = new SqlConnection("server=.;database=Data0720;user=sa;pwd=123;"); cmd = conn.CreateCommand(); } public string select(string code) { string end = "<无>"; cmd.CommandText = "select * from Nation where [email protected]"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a",code); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)//如果有数据 { dr.Read(); end = dr["nationname"].ToString(); } else { } conn.Close(); return end; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using 实体类_数据访问类.App_Code; using System.Data.SqlClient; namespace 实体类_数据访问类 { class Program { static void Main(string[] args) { //打印出所有信息 while (true) { Console.Clear();//清屏 Console.WriteLine("======================人员信息表======================"); Console.WriteLine(); Console.WriteLine("用户名" + "\t" + "密码" + "\t" + "昵称" + "\t" + "性别" + "\t" + "生日" + " " + "民族"); //实例化,先引用空间名 SqlConnection conn = new SqlConnection("server=.;database=Data0720;user=sa;pwd=123"); //设置数据库中的表的操作 SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from Users"; //执行操作 conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Console.WriteLine(dr["username"] + "\t" + dr["password"] + "\t" + dr["nickname"] + "\t" + (((bool)dr["sex"]) ? "男" : "女") + "\t" + Convert.ToDateTime(dr["brithday"]).ToString("yyyy年MM月dd日") + "\t" + dr["nation"]); } conn.Close(); Console.WriteLine("-------------------------------------------------------------"); Console.Write("请输入您想执行的操作(1.添加 2.删除 3.修改 4.查询某条信息 5.查询全部信息):"); string n = Console.ReadLine(); //实例化 UsersData udata = new UsersData();//在这个类被实例化的时候UsersData()代表的就是UsersData类中的构造函数 if (n == "1") { //添加 #region //实例化 Users u = new Users();//构造函数是默认的,可以不写,在这个类被实例化的时候Users()代表的就是构造函数 Console.Write("请输入用户名:"); u.username = Console.ReadLine(); Console.Write("请输入密码:"); u.password = Console.ReadLine(); Console.Write("请输入昵称:"); u.nickname = Console.ReadLine(); Console.Write("请输入性别:"); u.sex = (Console.ReadLine() == "男"); Console.Write("请输入生日:"); u.brithday = Convert.ToDateTime(Console.ReadLine()); Console.Write("请输入民族:"); u.nation = Console.ReadLine(); //实例化 UsersData udata.Insert(u);//调用udata里面的Insert()方法 #endregion } else if (n == "2") { //删除 #region Console.Write("请输入要删除的用户名:"); string Uname = Console.ReadLine(); Users u2 = udata.select(Uname); if (u2 != null) { Console.WriteLine("已查到此用户,请执行删除!"); Console.ReadLine(); bool ok = udata.Delete(Uname); if (ok) { Console.WriteLine("删除成功!"); } else { Console.WriteLine("删除失败!"); } } else { Console.WriteLine("请核实用户名输入是否正确!"); } #endregion } else if (n == "3") { //修改 #region Users u3 = new Users(); Console.Write("请输入您要修改的用户名:"); u3.username = Console.ReadLine(); if (u3 != null) { Console.Write("已查到此用户,请进行修改!"); Console.ReadLine(); Console.Write("请输入修改后的密码:"); u3.password = Console.ReadLine(); Console.Write("请输入修改后的性别:"); u3.sex = (Console.ReadLine() == "男"); udata.Update(u3); } else { Console.WriteLine("未查到您输入的用户名,请核实后重新输入!"); } #endregion } else if (n == "4") { //查一条数据 #region Console.Write("请输入你要查找的用户名:"); string Uname = Console.ReadLine(); Users u = new UsersData().select(Uname); if (u != null) { Console.WriteLine("已查到此用户!"); Console.ReadLine(); Console.WriteLine("用户名" + "\t" + "密码" + "\t" + "昵称" + "\t" + "性别" + "\t" + "生日" + " " + "民族"); Console.Write(u.username + "\t" + u.password + "\t" + u.nickname + "\t" + u.sex + "\t" + u.brithday + "\t" + u.nation); Console.ReadLine(); } else { Console.WriteLine("未查到此用户信息,请核实后重新输入!"); } #endregion } else if (n == "5") { List<Users> ulist = new UsersData().select(); if (ulist.Count > 0)//如果有数据 { Console.WriteLine("用户名" + "\t" + "密码" + "\t" + "昵称" + "\t" + "性别" + "\t" + "生日" + " " + "民族"); //循环打印,用foreach foreach (Users u in ulist)//遍历,把每一个数据都拿出来看一下 { Console.WriteLine(u.username + "\t" + u.password + "\t" + u.nickname + "\t" + u.sex1 + "\t" + u.brithday1 + "\t" + u.nationname); } Console.ReadLine(); } } else { Console.WriteLine("您的输入有误,请重新输入!"); } } Console.ReadLine(); } } }
添加:
删除:
修改:
查询一条信息:
查询全部信息:
以上是关于实体类数据访问类属性扩展的主要内容,如果未能解决你的问题,请参考以下文章