首页 > 代码库 > 属性拓展,
属性拓展,
其实属性拓展和我们上一篇写的差不多只是多了在实体类再加了一个属性,以便于调用和展示自己想用的内容
我们还是用一个例子来对比一下
1实体类:
namespace 数据库信息获取判定.App_Code
{
class Users
{
SqlConnection conn = null;
SqlCommand cmd = null;
public Users()
{
conn = new SqlConnection("server=.;database=Class0928;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 usersname
{
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; }
}
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.AddWithValue("@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 clas
{
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.AddWithValue("@a", _Class);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
end =dr["Classname"].ToString();
}
conn.Close();
return end;
}
}
}
}
___________________________________________________________________________________________________________________________
2数据访问类:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;//注意引用
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 数据库信息获取判定.App_Code
{
class UsersData
{ SqlConnection conn = null;
SqlCommand cmd = null;
public UsersData()
{
conn = new SqlConnection("server=.;database=Class0928;user=sa;pwd=123");
cmd = conn.CreateCommand();
}
/// <summary>
/// 将数据添加到Users表中,返回true说明添加成功
/// </summary>
/// <param name="u">要添加到数据表中的Users对象</param>
/// <returns></returns>
public bool Insert(Users u)
{
bool ok = false;
int count = 0;
cmd.CommandText = "insert into users values(@a,@b,@c,@d,@e,@f,@g)";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@a", u.usersname);
cmd.Parameters.AddWithValue("@b", u.password);
cmd.Parameters.AddWithValue("@c", u.nickname);
cmd.Parameters.AddWithValue("@d", u.sex);
cmd.Parameters.AddWithValue("@e", u.birthday);
cmd.Parameters.AddWithValue("@f", u.nation);
cmd.Parameters.AddWithValue("@g", u.clas);
try
{
conn.Open();
count = cmd.ExecuteNonQuery();
}
catch(Exception ex) {
Console.WriteLine(ex); ok = false; }
finally
{
conn.Close();
}
if (count > 0)
ok = true;
return ok;
}
/// <summary>
/// 更改数据
/// </summary>
/// <param name="u"></param>
/// <returns></returns>
public bool Update(Users u)
{
bool ok = false;
int count = 0;
cmd.CommandText = "update users set usersname=@a,password=@b,nickname=@c,sex=@d,birthday=@e,nation=@f,class=@g where usersname=@a";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@a", u.usersname);
cmd.Parameters.AddWithValue("@b", u.password);
cmd.Parameters.AddWithValue("@c", u.nickname);
cmd.Parameters.AddWithValue("@d", u.sex);
cmd.Parameters.AddWithValue("@e", u.birthday);
cmd.Parameters.AddWithValue("@f", u.nation);
cmd.Parameters.AddWithValue("@g", u.clas);
try
{
conn.Open();
count = cmd.ExecuteNonQuery();
}
catch(Exception ex) {
Console.WriteLine(ex); ok = false; }
finally
{
conn.Close();
}
if (count > 0)
ok = true;
return ok;
}
/// <summary>
/// 查询数据库信息
/// </summary>
/// <param name="usersname"></param>
/// <returns></returns>
public List<Users> Select()
{
List<Users> list = new List<Users>();
cmd.CommandText = "select *from Users";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Users u = new Users();
u.ids = Convert.ToInt32(dr["ids"]);
u.usersname = dr["usersname"].ToString();
u.password = dr["password"].ToString();
u.nickname = dr["nickname"].ToString();
u.sex = Convert.ToBoolean(dr["sex"]);
u.birthday = Convert.ToDateTime(dr["birthday"]);
u.nation = dr["nation"].ToString();
u.clas = dr["class"].ToString();
list.Add(u);
}
}
conn.Close();
return list;
}
public bool Select(string usersname)
{
bool has = false;
cmd.CommandText = "select *from users where usersname = @a";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@a", usersname);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
has = true;
conn.Close();
return has;
}
/// <summary>
/// 删除数据库信息
/// </summary>
/// <param name="uname"></param>
public bool Delete(string uname)
{
bool ok = false;
cmd.CommandText = "delete from users where usersname = @a";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@a", uname);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
ok = true;
else
ok = false;
return ok;
}
}
}
___________________________________________________________________________________________________________________________
3显示层:
for (; ; )
{
Console.WriteLine(" " + "=========================================信息展示======================================");
List<Users> ulist = new UsersData().Select();//实例化一个集合。调用
Console.WriteLine(" " + "编号" + "\t" + "用户名" + "\t" + " 密码" + "\t" + "昵称" + "\t" + "性别" + "\t" + " 生日" + "\t" + " 民族" + "\t" + " 班级" + "\t");
foreach (Users uuu in ulist)
{
Console.WriteLine(" " + uuu.ids + " \t" + uuu.usersname + "\t" + uuu.password + "\t" + uuu.nickname + "\t" + " " + uuu.SexStr + "\t" + uuu.BirthdayStr + "\t" + uuu.NationName + "\t" + uuu.ClassName);
}
Console.WriteLine();
Console.Write(" " + "请输入您想实现的功能 1 删除数据 2 添加数据 3 更改数据 请选择:");//功能选择。1 2 3
string m = Console.ReadLine();
if (m == "1")
{
Console.Write(" " + "请输入要删除的用户名:");
string uname = Console.ReadLine();
if (new UsersData().Select(uname))
{
Console.Write(" " + "已查到此用户信息,是否要删除?(Y/N)");
string yn = Console.ReadLine();
if (yn.ToUpper() == "Y")
{
new UsersData().Delete(uname);
Console.WriteLine(" " + "删除成功!");
}
else
{
Console.WriteLine(" " + "输入有误,请重新输入");
}
}
else
{
Console.WriteLine(" " + "输入有误,请重新输入");
}
}
else if (m == "2")
{
Users userm = new Users();//吧我们的封装的users类
Console.Write(" " + "请输入用户名:");
userm.usersname = Console.ReadLine();
Console.Write(" " + "请输入密码:");
userm.password = Console.ReadLine();
Console.Write(" " + "请输入昵称:");
userm.nickname = Console.ReadLine();
Console.Write(" " + "请输入性别:");
userm.sex = Convert.ToBoolean(Console.ReadLine());
Console.Write(" " + "请输入生日:");
userm.birthday = Convert.ToDateTime(Console.ReadLine());
Console.Write(" " + "请输入民族:");
userm.nation = Console.ReadLine();
Console.Write(" " + "请输入班级:");
userm.clas = Console.ReadLine();
UsersData ud = new UsersData();//吧usersdata类拿过来 引入一个bool类型
bool isok = ud.Insert(userm);
if (isok)
Console.WriteLine(" " + "添加成功!");
else
Console.WriteLine(" " + "添加失败!");
}
else if (m == "3")
{
Users userm = new Users();
Console.Write(" " + "请输入用户名:");
userm.usersname = Console.ReadLine();
if (new UsersData().Select(userm.usersname))
{
Console.Write(" " + "已查到此用户信息,是否要更改?(Y/N)");
string yn = Console.ReadLine();
if (yn.ToUpper() == "Y")
{
Console.Write(" " + "请输入密码:");
userm.password = Console.ReadLine();
Console.Write(" " + "请输入昵称:");
userm.nickname = Console.ReadLine();
Console.Write(" " + "请输入性别:");
userm.sex = Convert.ToBoolean(Console.ReadLine());
Console.Write(" " + "请输入生日:");
userm.birthday = Convert.ToDateTime(Console.ReadLine());
Console.Write(" " + "请输入民族:");
userm.nation = Console.ReadLine();
Console.Write(" " + "请输入班级:");
userm.clas = Console.ReadLine();
}
UsersData ud = new UsersData();
bool isok = ud.Update(userm);
if (isok)
Console.WriteLine(" " + "更改成功!");
else
Console.WriteLine(" " + "更改失败");
}
else
{
Console.WriteLine(" " + "输入有误,请重新输入");
}
}
else
{
Console.WriteLine(" " + "输入有误请重新输入");
}
List<Users> blist = new UsersData().Select();
Console.WriteLine(" " + "========================================最新信息展示========================================");
Console.WriteLine(" " + "编号" + "\t" + "用户名" + "\t" + " 密码" + "\t" + "昵称" + "\t" + "性别" + "\t" + " 生日" + "\t" + " 民族" + "\t" + " 班级" + "\t"); foreach (Users uuu in blist)
{
Console.WriteLine(" " + uuu.ids + " \t" + uuu.usersname + "\t" + uuu.password + "\t" + uuu.nickname + "\t" + " " + uuu.SexStr + "\t" + uuu.BirthdayStr + "\t" + uuu.NationName + "\t" + uuu.ClassName);
}
Console.ReadKey();
}
}
}
}
___________________________________________________________________________________________________________________________
显示如下:
属性拓展,