第十四章

来源:互联网 发布:能直接注册三级域名吗 编辑:程序博客网 时间:2024/05/01 13:38
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;namespace MySchoolBase{    class DBOperation    {        //连接字符串        private const string conn = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";        SqlConnection con = new SqlConnection(conn);        static void Main(string[] args)        {           DBOperation dno=new DBOperation();           dno.login();                                 Console.ReadLine();        }        public void login()        {            Console.WriteLine("请输入用户名");            string name = Console.ReadLine();            Console.WriteLine("请输入密码");            string mima = Console.ReadLine();            bool r=Check(name, mima);            if (r)            {                Console.WriteLine("登录成功");                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: 退出");                int n=int.Parse(Console.ReadLine());                switch(n)                {                    case 1:                        Amout();                        break;                    case 2:                    case 3:                    case 0:                        break;                }            }            else {                Console.WriteLine("登录失败");                                    }        }        public bool Check(string name,string mima)        {             //创建数据库             SqlConnection con = new SqlConnection(conn);              try            {                   string stra="SELECT COUNT(*) FROM Admin where LoginId='"+name+"' and LoginPwd='"+mima+"'";                con.Open();                  SqlCommand comm=new SqlCommand(stra,con);                  int i=(int)comm.ExecuteScalar();                                               }            catch (Exception ex)            {                Console.WriteLine("出现异常"+ex.Message);            }            finally            {              con.Close();                Console.WriteLine("关闭数据库");            }        return true;        }        public int Amout()        {            try            {                string stra = "SELECT COUNT(*) FROM Student";                con.Open();                SqlCommand comm = new SqlCommand(stra, con);                int i = (int)comm.ExecuteScalar();                Console.WriteLine("在校学生人数"+i);                return i;            }            catch (Exception)            {                return -1;            }            con.Close();        }    }}

0 0