博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ado.net 属性扩展 综合练习
阅读量:5881 次
发布时间:2019-06-19

本文共 17834 字,大约阅读时间需要 59 分钟。

实现数据查询,添加,删除,修改各项功能

 

业务逻辑层:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using 实体类_数据访问类练习.App_Code;namespace 实体类_数据访问类练习{    class Program    {        static void Main(string[] args)        {            for (; ; )            {                Console.WriteLine("请输入您要选择的操作:1、查询 2、添加 3、删除 4、修改 0、退出 ");                string operate = Console.ReadLine();                //查询                if (operate == "1")                {                    Console.WriteLine("");                    Console.Write("请输入您要查询的用户名(查询所有人员信息请输入*,退出请输入0。):");                    string operate_select = Console.ReadLine();                    if (operate_select == "*")                    {                        //完整查询操作,创建泛型数组引用UsersDate的Select方法                        List
list = new UsersData().Select(); //List
list = new List
(); //UsersData ud = new UsersData(); //list = ud.Select(); //遍历数组 Console.WriteLine(""); Console.WriteLine("============================数据信息=============================="); Console.WriteLine("序号\t用户名\t密码\t昵称\t性别\t民族\t班级\t生日"); foreach (Users uuu in list) { //Console.WriteLine(uuu.Code + "\t" + uuu.Name + "\t" + uuu.Password + "\t" + uuu.Nick + "\t" + (uuu.Sex ? "男" : "女") + "\t" + uuu.Nation + "\t" + uuu.Class + "\t" + Convert.ToDateTime(uuu.Birthday).ToShortDateString()); Console.WriteLine(uuu.Code + "\t" + uuu.Name + "\t" + uuu.Password + "\t" + uuu.Nick + "\t" + uuu.SexStr + "\t" + uuu.NationName + "\t" + uuu.ClassName + "\t" + uuu.BirthdayStr); Console.WriteLine(""); } } else if (operate_select == "0") { break; } else { //完整查询操作,创建泛型数组引用UsersDate的Select方法 List
list = new UsersData().Select(operate_select); //List
list = new List
(); //UsersData ud = new UsersData(); //list = ud.Select(); //遍历数组 Console.WriteLine(""); Console.WriteLine("============================数据信息=============================="); Console.WriteLine("序号\t用户名\t密码\t昵称\t性别\t民族\t班级\t生日"); foreach (Users uuu in list) { //Console.WriteLine(uuu.Code + "\t" + uuu.Name + "\t" + uuu.Password + "\t" + uuu.Nick + "\t" + (uuu.Sex ? "男" : "女") + "\t" + uuu.Nation + "\t" + uuu.Class + "\t" + Convert.ToDateTime(uuu.Birthday).ToShortDateString()); Console.WriteLine(uuu.Code + "\t" + uuu.Name + "\t" + uuu.Password + "\t" + uuu.Nick + "\t" + uuu.SexStr + "\t" + uuu.NationName + "\t" + uuu.ClassName + "\t" + uuu.BirthdayStr);//引用扩展属性 Console.WriteLine(""); } } } //添加用户操作 else if (operate == "2") { Console.WriteLine(""); Users user = new Users(); Console.Write("请输入要添加的用户名:"); user.Name = Console.ReadLine(); Console.Write("请输入要添加的密码:"); user.Password = Console.ReadLine(); Console.Write("请输入要添加的昵称:"); user.Nick = Console.ReadLine(); Console.Write("请输入要添加的性别:"); user.Sex = Convert.ToBoolean(Console.ReadLine()); Console.Write("请输入要添加的民族:"); user.Nation = Console.ReadLine(); Console.Write("请输入要添加的班级:"); user.Class = Console.ReadLine(); Console.Write("请输入要添加的生日:"); user.Birthday = Convert.ToDateTime(Console.ReadLine()); if (new UsersData().Insert(user))//数据访问类中insert方法的返回值 { Console.WriteLine("添加成功!"); } else { Console.WriteLine("添加失败!"); } Console.WriteLine(""); } //删除用户操作 else if (operate == "3") { Console.WriteLine(""); Console.Write("请输入你要删除的用户名:"); string operate_delete = Console.ReadLine();//用户操作 //判断是否插寻到此用户信息,返回TRUE表示有此用户信息 if (new UsersData().select(operate_delete)) { Console.WriteLine("查询到此用户信息,是否删除(Y/N):"); string yn = Console.ReadLine();//用户操作 if (yn.ToUpper() == "Y") { //判断删除此用户信息是否成功,返回TRUE表示删除成功 if (new UsersData().Delete(operate_delete)) { Console.WriteLine("删除成功!"); } //返回false表示删除失败 else { Console.WriteLine("删除失败!!"); } } else { Console.WriteLine("删除操作去取消!!"); } } //判断是否插寻到此用户信息,返回false表示无此用户信息 else { Console.WriteLine("未查询到此用户!!"); } Console.WriteLine(""); } //修改用户信息操作 else if (operate == "4") { Console.WriteLine(""); for (; ; ) { Console.Write("请输入要修改的用户名(输入0退出修改返回上一级):"); string operate_updata = Console.ReadLine(); if(operate_updata=="0") { break; } //查询是否有此用户,返回TRUE表示有此用户 if (new UsersData().select(operate_updata)) { Console.WriteLine("查询到此用户信息,是否修改(Y/N):"); string yn = Console.ReadLine();//用户操作 if (yn.ToUpper() == "Y") { Console.WriteLine ("请选择:*、修改用户所有信息;1、修改用户密码;2、修改用户昵称;3、修改用户性别;4、修改用户民族;5、修改用户班级;6、修改用户生日;0、结束修改"); string updata_opt = Console.ReadLine();//用户操作 //修改用户所有信息 if(updata_opt=="*") { Users us = new Users(); Console.Write("请输入要修改的密码:"); us.Password = Console.ReadLine(); Console.Write("请输入要修改的昵称:"); us.Nick = Console.ReadLine(); Console.Write("请输入要修改的性别:"); us.Sex = Convert.ToBoolean(Console.ReadLine()); Console.Write("请输入要修改的民族:"); us.Nation = Console.ReadLine(); Console.Write("请输入要修改的班级:"); us.Class = Console.ReadLine(); Console.Write("请输入要修改的生日:"); us.Birthday = Convert.ToDateTime(Console.ReadLine()); if (new UsersData().Update(us)) { Console.WriteLine("修改成功!!"); } else { Console.WriteLine("修改失败!!"); } } //修改用户密码 else if(updata_opt=="1") { Users us = new Users(); Console.Write("请输入要修改的密码:"); us.Password = Console.ReadLine(); if (new UsersData().Update1(us)) { Console.WriteLine("修改成功!!"); } else { Console.WriteLine("修改失败!!"); } } //修改用户昵称 else if (updata_opt == "2") { Users us = new Users(); Console.Write("请输入要修改的昵称:"); us.Nick = Console.ReadLine(); if (new UsersData().Update2(us)) { Console.WriteLine("修改成功!!"); } else { Console.WriteLine("修改失败!!"); } } //修改性别 else if (updata_opt == "3") { Users us = new Users(); Console.Write("请输入要修改的性别:"); us.Sex = Convert.ToBoolean(Console.ReadLine()); if (new UsersData().Update3(us)) { Console.WriteLine("修改成功!!"); } else { Console.WriteLine("修改失败!!"); } } //修改民族 else if (updata_opt == "4") { Users us = new Users(); Console.Write("请输入要修改的民族:"); us.Nation= Console.ReadLine(); if (new UsersData().Update4(us)) { Console.WriteLine("修改成功!!"); } else { Console.WriteLine("修改失败!!"); } } //修改班级 else if (updata_opt == "5") { Users us = new Users(); Console.Write("请输入要修改的班级:"); us.Class = Console.ReadLine(); if (new UsersData().Update5(us)) { Console.WriteLine("修改成功!!"); } else { Console.WriteLine("修改失败!!"); } } //修改生日 else if (updata_opt == "6") { Users us = new Users(); Console.Write("请输入要修改的生日:"); us.Birthday = Convert.ToDateTime(Console.ReadLine()); if (new UsersData().Update6(us)) { Console.WriteLine("修改成功!!"); } else { Console.WriteLine("修改失败!!"); } } //退出修改操作 else { break; } } else { Console.WriteLine("修改操作去取消!!"); } } //查询是否有此用户,返回false表示无此用户 else { Console.WriteLine("未查询到此用户!!"); } } Console.WriteLine(""); } //结束操作,退出 else { break; } Console.WriteLine(""); } Console.ReadLine(); } }}
View Code

实体类:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;namespace 实体类_数据访问类练习.App_Code{    public class Users    {        SqlConnection conn = null;        SqlCommand comm = null;        public Users()        {            conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");            comm = conn.CreateCommand();        }        private int _Code;        ///         /// code        ///         public int Code        {            get { return _Code; }            set { _Code = value; }        }        private string _Name;        ///         /// 用户名        ///         public string Name        {            get { return _Name; }            set { _Name = value; }        }        private string _Password;        ///         /// 密码        ///         public string Password        {            get { return _Password; }            set { _Password = value; }        }        private string _Nick;        ///         /// 昵称        ///         public string Nick        {            get { return _Nick; }            set { _Nick = value; }        }        private bool _Sex;        ///         /// 性别        ///         public bool Sex        {            get { return _Sex; }            set { _Sex = value; }        }        ///         ///性别的扩展属性        ///         public string SexStr        {            get            {                return _Sex ? "男" : "女";            }        }        private string _Nation;        ///         /// 民族        ///         public string Nation        {            get { return _Nation; }            set { _Nation = value; }        }        ///         /// 民族的扩展属性        ///         public string NationName        {            get            {                string end = "未填写";                comm.CommandText = "select *from Nation where nationcode=@a";                comm.Parameters.Clear();                comm.Parameters.AddWithValue("@a", _Nation);                conn.Open();                SqlDataReader dr = comm.ExecuteReader();                if (dr.HasRows)                {                    dr.Read();                    end = dr["nationname"].ToString();                }                conn.Close();                return end;            }        }        private string _Class;        ///         /// 班级        ///         public string Class        {            get { return _Class; }            set { _Class = value; }        }        ///         /// 班级的扩展属性        ///         public string ClassName        {            get            {                string end = "未填写";                comm.CommandText = "select *from Class where classcode=@a";                comm.Parameters.Clear();                comm.Parameters.AddWithValue("@a", _Class);                conn.Open();                SqlDataReader dr = comm.ExecuteReader();                if (dr.HasRows)                {                    dr.Read();                    end = dr["classname"].ToString();                }                conn.Close();                return end;            }        }        private DateTime _Birthday;        ///         /// 生日        ///         public DateTime Birthday        {            get { return _Birthday; }            set { _Birthday = value; }        }        ///         /// 生日的扩展属性        ///         public string BirthdayStr        {            get            {                return _Birthday.ToString("yyyy年MM月dd日");            }        }    }}
View Code

数据访问类

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;namespace 实体类_数据访问类练习.App_Code{    public class UsersData    {        SqlConnection conn = null;        SqlCommand comm = null;        public UsersData()        {            conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");            comm = conn.CreateCommand();        }        ///         /// 查询所有数据        ///         /// 
public List
Select() { List
list = new List
(); //comm.CommandText = "select code,usename,password,nick,sex,(select nationname from Nation where nationcode=Users.nation) as 'nation' ,classname as 'class',birthday from Users join Class on Class.classcode=Users.class"; comm.CommandText = "select * from Users"; conn.Open(); SqlDataReader dr = comm.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Users u = new Users(); u.Code = Convert.ToInt32(dr["Code"]); u.Name = dr["USENAME"].ToString(); u.Password = dr["Password"].ToString(); u.Nick = dr["Nick"].ToString(); u.Sex = Convert.ToBoolean(dr["Sex"]); u.Nation = dr["Nation"].ToString(); u.Class = dr["Class"].ToString(); u.Birthday = Convert.ToDateTime(dr["Birthday"]); list.Add(u); } } conn.Close(); return list; } ///
/// 查询单个用户的数据 /// ///
///
public List
Select(string username) { List
ulist = new List
(); //comm.CommandText = "select code,usename,password,nick,sex,(select nationname from Nation where nationcode=Users.nation) as 'nation' ,classname as 'class',birthday from Users join Class on Class.classcode=Users.class where usename='" + username + "'"; comm.CommandText = "select *from Users where usename='" + username + "'"; conn.Open(); SqlDataReader dr = comm.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Users u = new Users(); u.Code = Convert.ToInt32(dr["Code"]); u.Name = dr["USEName"].ToString(); u.Password = dr["Password"].ToString(); u.Nick = dr["Nick"].ToString(); u.Sex = Convert.ToBoolean(dr["Sex"]); u.Nation = dr["Nation"].ToString(); u.Class = dr["Class"].ToString(); u.Birthday = Convert.ToDateTime(dr["Birthday"]); ulist.Add(u); } } conn.Close(); return ulist; } ///
/// 将数据添加到Users表中 /// ///
要添加到表中的Users对象 ///
添加成功返回true,失败返回false
public bool Insert(Users us) { bool ok=false; int count = 0; comm.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f,@g)"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@a",us.Name); comm.Parameters.AddWithValue("@b", us.Password); comm.Parameters.AddWithValue("@c", us.Nick); comm.Parameters.AddWithValue("@d", us.Sex); comm.Parameters.AddWithValue("@e", us.Nation); comm.Parameters.AddWithValue("@f", us.Class); comm.Parameters.AddWithValue("@g", us.Birthday); try { conn.Open(); count = comm.ExecuteNonQuery(); } catch { ok = false; } finally { conn.Close(); } if (count > 0) ok = true; else ok = false; return ok; } ///
/// 完整删除和完整修改中,查询此用户是否存在 /// ///
要查询的Users对象 ///
返回TRUE表示由此用户
public bool select(string username) { bool has = false; comm.CommandText = "select *from Users where usename=@a"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@a",username); conn.Open(); SqlDataReader dr = comm.ExecuteReader(); if(dr.HasRows) { has = true; } conn.Close(); return has; } ///
/// 完整删除中查到用户后执行的删除操作 /// ///
要删除的Users对象< ///
返回true表示删除成功
public bool Delete(string uname) { bool have = false; int count_del = 0; comm.CommandText = "delete from Users where usename=@a"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@a",uname); try { conn.Open(); count_del = comm.ExecuteNonQuery(); } catch { have = false; } finally { conn.Close(); } if (count_del > 0) have = true; return have; } ///
/// 完整修改用户所有信息中查到用户后执行的修改操作 /// ///
///
public bool Update(Users user) { bool ok = false; int count = 0; comm.CommandText = "update Users set password=@b,nick=@c,sex=@d,nation=@e,class=@f,birthday=@g"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@b",user.Password); comm.Parameters.AddWithValue("@c", user.Nick); comm.Parameters.AddWithValue("@d", user.Sex); comm.Parameters.AddWithValue("@e", user.Nation); comm.Parameters.AddWithValue("@f", user.Class); comm.Parameters.AddWithValue("@g", user.Birthday); try { conn.Open(); count = comm.ExecuteNonQuery(); } catch { ok = false; } finally { conn.Close(); } if(count>0) { ok=true; } return ok; } ///
/// 完整修改用户中查到用户后执行的单独修改密码操作 /// ///
///
public bool Update1(Users user) { bool ok = false; int count = 0; comm.CommandText = "update Users set password=@b"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@b", user.Password); try { conn.Open(); count = comm.ExecuteNonQuery(); } catch { ok = false; } finally { conn.Close(); } if (count > 0) { ok = true; } return ok; } ///
/// 完整修改用户中查到用户后执行的单独修改昵称操作 /// ///
///
public bool Update2(Users user1) { bool ok = false; int count = 0; comm.CommandText = "update Users set nick=@b"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@b", user1.Nick); try { conn.Open(); count = comm.ExecuteNonQuery(); } catch { ok = false; } finally { conn.Close(); } if (count > 0) { ok = true; } return ok; } ///
/// 完整修改用户中查到用户后执行的单独修改性别操作 /// ///
///
public bool Update3(Users user1) { bool ok = false; int count = 0; comm.CommandText = "update Users set sex=@b"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@b", user1.Sex); try { conn.Open(); count = comm.ExecuteNonQuery(); } catch { ok = false; } finally { conn.Close(); } if (count > 0) { ok = true; } return ok; } ///
/// 完整修改用户中查到用户后执行的单独修改民族操作 /// ///
///
public bool Update4(Users user1) { bool ok = false; int count = 0; comm.CommandText = "update Users set nation=@b"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@b", user1.Nation); try { conn.Open(); count = comm.ExecuteNonQuery(); } catch { ok = false; } finally { conn.Close(); } if (count > 0) { ok = true; } return ok; } ///
/// 完整修改用户中查到用户后执行的单独修改班级操作 /// ///
///
public bool Update5(Users user1) { bool ok = false; int count = 0; comm.CommandText = "update Users set class=@b"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@b", user1.Class); try { conn.Open(); count = comm.ExecuteNonQuery(); } catch { ok = false; } finally { conn.Close(); } if (count > 0) { ok = true; } return ok; } ///
/// 完整修改用户中查到用户后执行的单独修改生日操作 /// ///
///
public bool Update6(Users user1) { bool ok = false; int count = 0; comm.CommandText = "update Users set birthday=@b"; comm.Parameters.Clear(); comm.Parameters.AddWithValue("@b", user1.Birthday); try { conn.Open(); count = comm.ExecuteNonQuery(); } catch { ok = false; } finally { conn.Close(); } if (count > 0) { ok = true; } return ok; } }}
View Code

 

转载于:https://www.cnblogs.com/maxin991025-/p/6121990.html

你可能感兴趣的文章
memcached 分布式聚类算法
查看>>
jquery css3问卷答题卡翻页动画效果
查看>>
$digest already in progress 解决办法——续
查看>>
虚拟机 centos设置代理上网
查看>>
Struts2中Date日期转换的问题
查看>>
mysql 数据类型
查看>>
Ubuntu 设置当前用户sudo免密码
查看>>
设置tomcat远程debug
查看>>
android 电池(一):锂电池基本原理篇【转】
查看>>
Total Command 常用快捷键
查看>>
ionic 调用手机的打电话功能
查看>>
怎么使用阿里云直播服务应用到现在主流直播平台中
查看>>
Xcode全局替换内容,一键Replace
查看>>
1000 加密算法
查看>>
exif_imagetype() 函数在linux下的php中不存在
查看>>
Ruby的case语句
查看>>
Linux的链接文件-ln命令
查看>>
maven的tomcat插件如何进行debug调试
查看>>
table表头固定
查看>>
截取字符串中两个字符串中的字符串
查看>>