1. 程式人生 > >ACCP S1 C#第十四章 第十五章 上機練習

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();
            }
        }
        }
    }