数据库技术基础

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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值