using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// 执行TSQL语句
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public int zhixing(string str)
        {
            conn.Open();
            try
            {
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = str;
                int i = cmd.ExecuteNonQuery();
            conn.Close();
                return i;
            }
            catch (Exception ex) { Console.WriteLine(ex.Message);
            conn.Close(); ; }
        }
        static SqlConnection conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=123");

        /// <summary>
        /// 判断数据输入是否有误
        /// </summary>
        /// <param name="id">1:密码 2:昵称 3:性别 4:生日 5:民族 6:班级</param>
        /// <returns></returns>
        public string panduan(int id)
        {
            switch (id)
            {
                :
                    //密码
                    for (; ; )
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入密码:");
                        string napw = Console.ReadLine();
                        if (napw != "")
                            return napw;
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("该字段不能为空");
                        }
                    }
                :
                    //昵称
                    for (; ; )
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入昵称:");
                        string napw = Console.ReadLine();
                        if (napw != "")
                            return napw;
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("该字段不能为空");
                        }
                    }
                :
                    //性别
                    for (; ; )
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入性别:");
                        string sex = Console.ReadLine();
                        if (sex != "")
                        {
                            " || sex == "雄")
                                return "True";
                            " || sex == "雌")
                                return "False";
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("性别输入有误");
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("该字段不能为空");
                        }
                    }
                :
                    //生日
                    for (; ; )
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入生日:");
                        try   //尝试去做,如果有错,从错误的句直接跳转到catch
                        {
                            DateTime zhi = DateTime.Parse(Console.ReadLine());
                            return zhi.ToShortDateString();
                        }
                        catch  //只有错误的情况下
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("输入日期格式错误");
                        }
                    }
                :
                    //民族
                    for (; ; )
                    {
                        selectNation("Nation");
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入民族:N");
                        string nation = "";
                        int i = Convert.ToInt32(Console.ReadLine());
                        )
                            i = Math.Abs(i);
                        )
                        {
                            nation = ");

                            if (boselect("Nation", "NationCode", nation) || boselect("Nation", "NationName", nation))
                            {
                                return nation;
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("没有此民族编号或名称");
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("请输入数字1~999之间");
                        }
                    }
                :
                    //班级
                    for (; ; )
                    {
                        selectNation("Class");
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入班级:C");
                        string nation = "";
                        int i = Convert.ToInt32(Console.ReadLine());
                        )
                            i = Math.Abs(i);
                        )
                        {
                            nation = ");

                            if (boselect("Class", "ClassCode", nation) || boselect("Class", "ClassName", nation))
                            {
                                return nation;
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("没有此班级编号或名称");
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("请输入数字1~999之间");
                        }
                    }
                default: break;
            }
            return "";
        }

        /// <summary>
        /// 判断要执行的操作
        /// </summary>
        /// <param name="id">操作编号</param>
        public void caozuo(int id)
        {
            switch (id)
            {
                :
                    //查询select
                    while (true)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入要查看的表名:");
                        string tabname = Console.ReadLine();
                        if (tabname != "")
                        {
                            Console.Write("请输入条件列名:");
                            string cluame = Console.ReadLine();
                            if (tabname != "")
                            {
                                Console.Write("请输入条件:");
                                string UserName = Console.ReadLine();
                                if (tabname != "")
                                {
                                    if (select(tabname, cluame, UserName))
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("该字段不能为空");
                        }
                    }
                    break;
                :
                    //插入
                    while (true)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入要插入的用户名:");
                        string UserName = Console.ReadLine();
                        if (!boselect("Users", "UserName", UserName))
                        {
                            );
                            );
                            );
                            );
                            );
                            );
                            )
                            {
                                Console.WriteLine("成了"); break;
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("没成");
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("此用户名以存在请更改。");
                        }
                    }
                    break;
                :
                    //删除
                    while (true)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入要删除的用户名:");
                        string UserName = Console.ReadLine();
                        if (boselect("Users", "UserName", UserName))
                        {
                            )
                            {
                                Console.WriteLine("成了"); break;
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("没成");
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("此用户名不存在请更改。");
                        }
                    }
                    break;
                :
                    //修改
                    while (true)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入要修改的用户名:");
                        string UserName = Console.ReadLine();
                        if (boselect("Users", "UserName", UserName))
                        {
                            );
                            );
                            );
                            );
                            );
                            );
                            )
                            {
                                Console.WriteLine("成了"); break;
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("没成");
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("此用户名不存在请更改。");
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 查询语句
        /// </summary>
        /// <param name="tabname">要查询的表名</param>
        /// <param name="cluame">要查询的条件列</param>
        /// <param name="UserName">条件</param>
        /// <returns></returns>
        public bool select(string tabname, string cluame, string UserName)
        {
            try
            {
                SqlCommand cmd = conn.CreateCommand();
                Console.ForegroundColor = ConsoleColor.Blue;
                if (tabname == "Users" || tabname == "users")
                {
                    conn.Open();
                    cmd.CommandText = "SELECT ids,UserName,[PassWord],NickName,Sex,Birthday,NationName as Nation,ClassName AS Class FROM Users,Class,Nation WHERE Nation=NationCode AND Class=ClassCode AND " + cluame + "='" + UserName + "'";

                    SqlDataReader dr = cmd.ExecuteReader();
                    Console.WriteLine("┌──────────────────────────────────┐");
                    Console.WriteLine("│ ID\t用户名\t密码\t昵称\t性别\t生日\t\t民族\t班级 │");
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            Console.WriteLine("│ {0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7} │", dr["ids"], dr["username"], dr["PassWord"], dr["NickName"], dr["Sex"], Convert.ToDateTime(dr["Birthday"]).ToShortDateString(), dr["Nation"], dr["class"]);
                        }
                        Console.WriteLine("└──────────────────────────────────┘");
                        conn.Close();
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("│──────────────────────────────────│");
                        Console.WriteLine("│─────────────未查到任何数据──────────────│");
                        Console.WriteLine("│──────────────────────────────────│");
                        Console.WriteLine("└──────────────────────────────────┘");
                        conn.Close();
                        return false;
                    }
                }
                else
                {
                    cmd.CommandText = "SELECT * FROM " + tabname + " where " + cluame + "='" + UserName + "'";
                    SqlDataReader dr = cmd.ExecuteReader();
                    Console.WriteLine("┌───────────┐");
                    Console.WriteLine("│\t编号\t名称\t│");
                    conn.Open();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            Console.WriteLine(], dr[]);
                        }
                        Console.WriteLine("└───────────┘");
                        conn.Close();
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("│───────────│");
                        Console.WriteLine("│──未查到任何数据──│");
                        Console.WriteLine("│───────────│");
                        Console.WriteLine("└───────────┘");
                        conn.Close();
                        return false;
                    }
                }
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("表名、列名、数据输入有误");
                return false;
            }
        }

        /// <summary>
        /// 判断UserName是否存在
        /// </summary>
        /// <param name="tabname">表名</param>
        /// <param name="UserName">用户名</param>
        /// <returns></returns>
        public bool boselect(string tabname, string cluame, string UserName)
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT * FROM " + tabname + " where "+cluame+"='" + UserName + "'";
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                conn.Close();
                return true;
            }
            else
            {
                conn.Close();
                return false;
            }
        }

        /// <summary>
        /// 打印所有用户数据
        /// </summary>
        public void selectall()
        {
            SqlCommand cmd = conn.CreateCommand();
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("┌──────────────────────────────────┐");
            Console.WriteLine("│ ID\t用户名\t密码\t昵称\t性别\t生日\t\t民族\t班级 │");
            conn.Open();
            cmd.CommandText = "SELECT ids,UserName,[PassWord],NickName,Sex,Birthday,NationName as Nation,ClassName AS Class FROM Users,Class,Nation WHERE Nation=NationCode AND Class=ClassCode";
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Console.WriteLine("│ {0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7} │", dr["ids"], dr["username"], dr["PassWord"], dr["NickName"], dr["Sex"], Convert.ToDateTime(dr["Birthday"]).ToShortDateString(), dr["Nation"], dr["class"]);
                }
            }
            else
            {
                Console.WriteLine("│──────────────────────────────────│");
                Console.WriteLine("│─────────────未查到任何数据──────────────│");
                Console.WriteLine("│──────────────────────────────────│");
            }
            conn.Close();
            Console.WriteLine("└──────────────────────────────────┘");
        }

        /// <summary>
        /// 打印所有民族与代码
        /// </summary>
        public void selectNation(string tabname)
        {
            SqlCommand cmd = conn.CreateCommand();
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("┌───────────┐");
            Console.WriteLine("│\t编号\t名称\t│");
            conn.Open();
            cmd.CommandText = "SELECT * FROM " + tabname;
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Console.WriteLine(], dr[]);
                }
            }
            else
            {
                Console.WriteLine("│───────────│");
                Console.WriteLine("│──未查到任何数据──│");
                Console.WriteLine("│───────────│");
            }
            conn.Close();
            Console.WriteLine("└───────────┘");

        }

        static void Main(string[] args)
        {
            Program pr = new Program();
            SqlCommand cmd = conn.CreateCommand();

            //打印所有人的数据
            pr.selectall();
            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("1.查询");
                Console.WriteLine("2.插入");
                Console.WriteLine("3.删除");
                Console.WriteLine("4.修改");
                Console.Write("请选择要执行的操作:");
                try
                {
                    int biao = Convert.ToInt32(Console.ReadLine());
                     && biao < )
                    {
                        pr.caozuo(biao);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("请输入任意键继续_");
                        Console.ReadKey();
                        Console.Clear();
                        pr.selectall();
                    }
                    else
                    {
                        Console.Clear();
                        pr.selectall();
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("请输入1~4进行选择");
                    }
                }
                catch
                {
                    Console.Clear();
                    pr.selectall();
                    Console.WriteLine("请输入数字1~4进行选择");
                }
            }
        }
    }
}

代码

干净到爆,可以点啊,点点试试

2016年11月28日--ADO.Net 查、插、删、改 小练习的更多相关文章

  1. 2016年11月28日--ADO.Net 增、删、改、查

    数据访问 对应命名空间:System.Data.SqlClient; SqlConnection:连接对象SqlCommand:命令对象SqlDataReader:读取器对象 CommandText: ...

  2. 2016年11月28日 星期一 --出埃及记 Exodus 20:19

    2016年11月28日 星期一 --出埃及记 Exodus 20:19 and said to Moses, "Speak to us yourself and we will listen ...

  3. 2016年12月28日 星期三 --出埃及记 Exodus 21:23

    2016年12月28日 星期三 --出埃及记 Exodus 21:23 But if there is serious injury, you are to take life for life,若有 ...

  4. 2016年11月30日 星期三 --出埃及记 Exodus 20:21

    2016年11月30日 星期三 --出埃及记 Exodus 20:21 The people remained at a distance, while Moses approached the th ...

  5. 2016年11月29日 星期二 --出埃及记 Exodus 20:20

    2016年11月29日 星期二 --出埃及记 Exodus 20:20 Moses said to the people, "Do not be afraid. God has come t ...

  6. 2016年11月27日 星期日 --出埃及记 Exodus 20:18

    2016年11月27日 星期日 --出埃及记 Exodus 20:18 When the people saw the thunder and lightning and heard the trum ...

  7. 2016年11月26日 星期六 --出埃及记 Exodus 20:17

    2016年11月26日 星期六 --出埃及记 Exodus 20:17 "You shall not covet your neighbor's house. You shall not c ...

  8. 2016年11月25日 星期五 --出埃及记 Exodus 20:16

    2016年11月25日 星期五 --出埃及记 Exodus 20:16 "You shall not give false testimony against your neighbor.不 ...

  9. 2016年11月24日 星期四 --出埃及记 Exodus 20:15

    2016年11月24日 星期四 --出埃及记 Exodus 20:15 "You shall not steal.不可偷盗.

随机推荐

  1. bzoj[1087][SCOI2005]互不侵犯King

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  2. MVC之前的那点事儿系列(10):MVC为什么不再需要注册通配符(*.*)了?

    文章内容 很多教程里都提到了,在部署MVC程序的时候要配置通配符映射(或者是*.mvc)到aspnet_ISPAI.dll上,在.NET4.0之前确实应该这么多,但是.NET4.0之后已经不要再费事了 ...

  3. centos7.0 安装字体库

    最近在centos7.0下用itextpdf将word文档转成pdf时出现字体丢失的情况.网上找了很多资料,各式各样的原因和解决方法.后来经过一番测试发现是centos7.0 minimal没有安装相 ...

  4. 【笔记6】用pandas实现条目数据格式的推荐算法 (基于物品的协同)

    ''' 基于物品的协同推荐 矩阵数据 说明: 1.修正的余弦相似度是一种基于模型的协同过滤算法.我们前面提过,这种算法的优势之 一是扩展性好,对于大数据量而言,运算速度快.占用内存少. 2.用户的评价 ...

  5. 用C#实现封装

    用C#实现封装 1.属性对外公开类似于类的接口实现对字段的访问;2.字段为private只能在内部被直接访问,如果当属性为只读,那么可以将形参直接对字段赋值.(有没有更好的方法?);3.可以通过关键字 ...

  6. 【JavaScript】[bind,call,apply] (function cal(){}());声明函数立即执行

    ---恢复内容开始--- 1.js 里函数调用有 4 种模式:方法调用.正常函数调用.构造器函数调用.apply/call 调用.同时,无论哪种函数调用除了你声明时定义的形参外,还会自动添加 2 个形 ...

  7. 【平面设计AFTER】读到的设计海报分层法

    来源参考:http://www.uisdc.com/graphic-designer-self-improvement 分层法的三层:“背景层”,“图形层”,“信息层” 1,背景层,一般为纯色,场景, ...

  8. 根据浏览器显示界面大小变换,替换css文件方法

    在1024屏幕下,选择适配1024屏幕的css文件, 在大于1024屏幕下,选择适配大屏幕的css文件. 在html中的head标签中引用css文件时,加入media属性. 例: <link r ...

  9. VBScript使用CDO.Message发送邮件

    Const Email_From = "from@163.com" Const Password = "password" Const Email_To = & ...

  10. 怎么样修改小猪cms(从功能库添加)模块关键字

    需求:修改或者添加从功能库添加中的关键字 这里以添加咨询投诉为列: 找到wwwroot\PigCms\Lib\Action\User目录下的LinkAction.class.php文件(手动找不到直接 ...