数据库技术基础

来源:互联网 发布:软件直销网 编辑:程序博客网 时间:2024/06/15 14:05
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace sqlserven{    class Schoolman    {        DBO DB = new DBO();        public void Login() {            Console.WriteLine("请输入用户名");            int name =int.Parse(Console.ReadLine());            Console.WriteLine("请输入密码");            String pwd = Console.ReadLine();            bool b=DB.che(name, pwd);            if (b)            {                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("8.退出");                int i = int.Parse(Console.ReadLine());                xz(i);            }            else {                Console.WriteLine("cuo");            }        }        public void xz(int i) {            switch (i) {                 case 1:                    Console.WriteLine("一共"+DB.yi()+"人");                    break;                case 2:                    DB.er();                    break;                case 3:                    DB.san();                    break;                case 4:                    break;                case 5:                    DB.wu();                    break;                case 6:                    DB.liu();                    break;                case 7:                    DB.qi();                    break;                case 8:                    Console.WriteLine("退出成功");                    break;            }        }           }}


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;namespace sqlserven{    class DBO    {        string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";              public bool che(int name,String pwd)        {            SqlConnection conn = new SqlConnection(str);            try            {                              string sql = "select count(*) from student where StudentNo='" + name + "'and LoginPwd='" + pwd + "'";                conn.Open();                SqlCommand comm = new SqlCommand(sql, conn);                int red = (int)comm.ExecuteScalar();                                if (red ==0 )                {                       return false;                }                else                {                    return true;                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                return false;            }            finally {                conn.Close();            }                    }        public int yi()        {            SqlConnection conn = new SqlConnection(str);            try            {                string s = "select count(*) from student";                conn.Open();                SqlCommand comm = new SqlCommand(s, conn);                int i = (int)comm.ExecuteScalar();                               return i;            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                throw;            }            finally {                conn.Close();            }                  }        public void er() {            SqlConnection conn = new SqlConnection(str);            String s = "select * from student student";            conn.Open();            SqlCommand comm = new SqlCommand(s,conn);            SqlDataReader sql=comm.ExecuteReader();            while(sql.Read()){                Console.WriteLine(sql["studentname"]);            }                  }        public void san() {            Console.WriteLine("请输入学号");            int id = int.Parse(Console.ReadLine());            SqlConnection conn = new SqlConnection(str);            try            {                              String s = "select * from student where  studentno=' " + id + "'";                conn.Open();                SqlCommand comm = new SqlCommand(s, conn);                SqlDataReader sql = comm.ExecuteReader();                while (sql.Read())                {                    Console.WriteLine("学号{0},姓名{1}", sql["studentno"], sql["studentname"]);                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                throw;            }            finally {                conn.Close();            }        }        public void wu() {            Console.WriteLine("请输入学号");            int id = int.Parse(Console.ReadLine());             Console.WriteLine("请输入要修改的日期");            String time=Console.ReadLine();            DateTime ds = Convert.ToDateTime(time);            SqlConnection conn = new SqlConnection(str);            try            {                string s = "update student set Birthday='" + ds + "' where StudentNo='"+id+"'";                conn.Open();                SqlCommand comm = new SqlCommand(s,conn);                int count=comm.ExecuteNonQuery();                if(count>0){                    Console.WriteLine("修改成功");                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                throw;            }        }        public void liu() {            SqlConnection conn = new SqlConnection(str);            try            {                Console.WriteLine("请输入要删除的学号");                int i = int.Parse(Console.ReadLine());                string s = "delete from student where studentno='" + i + "'";                conn.Open();                SqlCommand comm = new SqlCommand(s, conn);                int y = comm.ExecuteNonQuery();                if (y > 0)                {                    Console.WriteLine("删除成功");                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                throw;            }            finally {                conn.Close();            }        }        public void qi() {            SqlConnection conn = new SqlConnection(str);            try            {                Console.WriteLine("请输入要添加的班级名");                String name = Console.ReadLine();                String s = "insert Grade(GradeName) values('"+name+"')";                conn.Open();                SqlCommand comm = new SqlCommand(s, conn);                int i = comm.ExecuteNonQuery();                if (i > 0)                {                    Console.WriteLine("添加成功");                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                throw;            }            finally {                conn.Close();            }        }    }}


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace sqlserven{    class Program    {        static void Main(string[] args)        {            Schoolman s = new Schoolman();            s.Login();            Console.ReadLine();        }    }}


 
原创粉丝点击