ACCP S1 C#第十四章 第十五章 上机练习

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

namespace shang_ji
{
    class Program
    {
        static void Main(string[] args)
        {
                   Program p = new Program();  
            string name="";  
            string pwd="";  
            p.login(ref name,ref pwd);  
            bool b=p.check(name,pwd);  
            if (b)  
            {  
                Console.WriteLine("登陆成功");  
                p.Login();  
            }  
            else {  
                Console.WriteLine("登陆无效");  
            }  
            Console.ReadLine();  
        }  
        public void login(ref string name,ref string pwd) {  
            Console.WriteLine("请输入用户名:");  
            name = Console.ReadLine();  
            Console.WriteLine("请输入密码:");  
            pwd = Console.ReadLine();  
        }  
        public bool check(string x,string y) {
            string s = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";  
            SqlConnection a = new SqlConnection(s);  
            a.Open();  
            string i = "select count(*) from Admin where LoginId='"+x+"'and LoginPwd='"+y+"'";  
            SqlCommand c = new SqlCommand(i,a);  
            int b = (int)c.ExecuteScalar();  
            try  
            {  
                if (b != 0)  
                {  
                    return true;  
                }  
                return false;  
            }  
            catch (Exception)  
            {  
                Console.WriteLine("运行错误");  
                return false;  
            }  
            finally {  
                a.Close();  
            }  
        }  
        public void Login() {  
            bool z=true;  
            do{  
            Console.WriteLine("==========请选择操作键==========");  
            Console.WriteLine("1.统计学生人数");  
            Console.WriteLine("2.查看学生名单");  
            Console.WriteLine("3.按学号查看学生姓名");  
            Console.WriteLine("4.按姓名查询学生信息");  
            Console.WriteLine("5.修改学生出生日期");  
            Console.WriteLine("6.删除学生记录");  
            Console.WriteLine("7.新增年级记录");  
            Console.WriteLine("0.退出");  
            Console.WriteLine("================================");  
            string choose =Console.ReadLine();  
                if(choose=="1"){
                    no1();
                }else if(choose=="3"){
                    no3();
                }
                else if (choose == "4")
                {
                    no4();
                }
                else if (choose == "5")
                {
                    no5();
                }
                else if (choose == "6")
                {
                    no6();
                }
                else if (choose == "7")
                {
                    no7();
                }
                else if (choose != "0")  
            {  
                continue;  
            }  
            else {  
                z = false;  
            }  
            }while(z);  
        }
        public void no1() {
            string s = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
            SqlConnection a = new SqlConnection(s);
            a.Open();
            try
            {
                string i = "select count(*) from Student";
                SqlCommand c = new SqlCommand(i, a);
                int b = (int)c.ExecuteScalar();
                Console.WriteLine("在校学生:" + b + "人");
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                a.Close();
            }  
        }
        public void no3() {
            Console.WriteLine("请输入学生学号:");
            string no = Console.ReadLine();
            string s = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
            SqlConnection a = new SqlConnection(s);
            a.Open();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select");
                sb.Append(" StudentName");
                sb.Append(" from");
                sb.Append(" Student");
                sb.Append(" where StudentNo='"+no+"'");
                SqlCommand m = new SqlCommand(sb.ToString(), a);
                SqlDataReader r = m.ExecuteReader();
                if (r.Read())
                {
                    Console.WriteLine("学号是" + no + "的学生姓名为:" + r["studentname"]);
                }
                else
                {
                    Console.WriteLine("出现异常");
                }
                Console.ReadLine();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                a.Close();
            }
        }
        public void no4() {
            Console.WriteLine("请输入学生姓名:");
            string no = Console.ReadLine();
            string s = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
            SqlConnection a = new SqlConnection(s);
            a.Open();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select");
                sb.Append(" *");
                sb.Append(" from");
                sb.Append(" Student");
                sb.Append(" where StudentName like '%"+no+"%'");
                SqlCommand m = new SqlCommand(sb.ToString(), a);
                SqlDataReader r = m.ExecuteReader();
                if (r.Read())
                {
                    Console.WriteLine("学号:" + r["studentno"] + " 姓名:" + r["studentname"]+" 性别:"+r["Sex"]);
                }
                else
                {
                    Console.WriteLine("出现异常");
                }
                Console.ReadLine();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                a.Close();
            }
        }
        public void no5() {
            Console.WriteLine("请输入学号:");
            string no = Console.ReadLine();
            Console.WriteLine("请输入修改后的生日:");
            string noo = Console.ReadLine();
            string s = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
            SqlConnection a = new SqlConnection(s);
            a.Open();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("update Student set BornDate='" + noo + "' where StudentNo='"+no+"'");
                SqlCommand m = new SqlCommand(sb.ToString(), a);
                int b = m.ExecuteNonQuery();
                if (b == 0)
                {
                    Console.WriteLine("修改失败");
                }
                else
                {
                    Console.WriteLine("修改成功");
                }
                Console.ReadLine();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                a.Close();
            }
        }
        public void no6()
        {
            Console.WriteLine("请输入学号:");
            string no = Console.ReadLine();
            string s = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
            SqlConnection a = new SqlConnection(s);
            a.Open();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("delete from Student where StudentNo='"+no+"'");
                SqlCommand m = new SqlCommand(sb.ToString(), a);
                int b = m.ExecuteNonQuery();
                if (b == 0)
                {
                    Console.WriteLine("删除失败");
                }
                else
                {
                    Console.WriteLine("删除成功");
                }
                Console.ReadLine();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                a.Close();
            }
        }
        public void no7() {
            Console.WriteLine("请输入待插入的年级名称:");
            string no = Console.ReadLine();
            string s = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
            SqlConnection a = new SqlConnection(s);
            a.Open();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("insert into Grade(GradeName) values ('" + no + "')");
                SqlCommand m = new SqlCommand(sb.ToString(), a);
                int b= m.ExecuteNonQuery();
                if (b==0)
                { 
                    Console.WriteLine("已存在该年级");
                }else{
                    Console.WriteLine("添加" + no+"成功");
                }
                Console.ReadLine();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                a.Close();
            }
        }
        }
    }


展开阅读全文

没有更多推荐了,返回首页