首页 > 代码库 > 把不同层面的数据联合起来的实例(实用)
把不同层面的数据联合起来的实例(实用)
1.建立一个新的文件夹App_Code
里边放一个实体类 叫做Users.封装起来
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 数据库信息获取判定.NewFolder1
{
class Users
{
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; }
}
private DateTime _Birthday;
/// <summary>
/// 生日
/// </summary>
public DateTime birthday
{
get { return _Birthday; }
set { _Birthday = value; }
}
private string _Nation;
/// <summary>
/// 民族
/// </summary>
public string nation
{
get { return _Nation; }
set { _Nation = value; }
}
private string _Class;
/// <summary>
/// 班级
/// </summary>
public string clas
{
get { return _Class; }
set { _Class = value; }
}
}
}
___________________________________________________________________________________________________________________________
2 在这个文件里同样建立一个UserData数据访问类
using System;
using System.Collections.Generic;
using System.Data.SqlClient;//注意引用
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 数据库信息获取判定.NewFolder1
{
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=1";
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在显示层
using System;
using System.Collections.Generic;
using System.Data.SqlClient;//注意引用
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 数据库信息获取判定.NewFolder1;//注意引用
namespace 数据库信息获取判定
{
class Program
{
static void Main(string[] args)
{
for (; ; )
{
Console.WriteLine(" " + "=========================================信息展示=========================================");
List<Users> ulist = new UsersData().Select();//实例化一个集合。调用
foreach (Users uuu in ulist)
{
Console.WriteLine(" " + uuu.ids + " \t" + uuu.usersname + "\t" + uuu.password + "\t" + uuu.nickname + "\t" + (uuu.sex ? "男" : "女") + "\t" + uuu.birthday + "\t" + uuu.nation + "\t" + uuu.clas);
}
Console.Write(" " + "请输入您想实现的功能 1 删除数据 2 添加数据 3 更改数据 请选择:");//功能选择。1 2 3
int m = int.Parse(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(" " + "========================================最新信息展示========================================");
foreach (Users uuu in blist)
{
Console.WriteLine(" " + uuu.ids + " \t" + uuu.usersname + "\t" + uuu.password + "\t" + uuu.nickname + "\t" + uuu.sex + "\t" + uuu.birthday + "\t" + uuu.nation + "\t" + uuu.clas);
}
Console.ReadKey();
}
}
}
}
___________________________________________________________________________________________________________________________
如图所示
把不同层面的数据联合起来的实例(实用)