第十五章

本文介绍了一个使用C#实现的学生信息管理系统,该系统包括登录验证、统计学生人数、查看学生名单等功能,并通过SQL Server数据库进行数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication2
{
    class Program
    { 
        string ent = @"Data Source=.;Initial Catalog=MySchool;Integrated Security=True ;User ID=KK;Password=12";

        static void Main(string[] args)
        {
            Program Q = new Program();
            Q.su();
           
        }

 public void su()  
       {  
            Console.WriteLine("请输入用户名:");  
            string name = Console.ReadLine();  
           Console.WriteLine("请输入密码:");  
          int pass =int.Parse(Console.ReadLine());  
          if (pass != 000)  
          {  
              Console.WriteLine("登录失败");  
          }  
          else  
         {  
              Console.WriteLine("登录成功");  
              cg();  
          }  
 
        }  
  
       public void cg()  
        {
            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("8.退出");
                Console.WriteLine("================================");
                Console.WriteLine("请选择输入数字:");
                int m = int.Parse(Console.ReadLine());
                if(m==8){
                    Console.WriteLine("退出!");
                    break;
                }
                switch (m)
                {
                    case 1:
                        Console.WriteLine("在校学生人数:" + qun());
                        break;
                    case 2:
                        QW();
                        break;
                    case 3:
                     show();
                        break;
                    case 4:

                        break;
                    case 5:

                        break;
                    case 6:

                        break;
                    case 7:

                        break;
                    case 8:
                        Console.WriteLine("退出!");
                        break;
                    default:
                        Console.WriteLine("输入错误,请重新输入:");
                        break;

                }
              
            } while (true);
           
         }

             public int qun()  
       {  
            SqlConnection tuy = new SqlConnection(ent);  
            try  
            {  
                tuy.Open();  
               string re = "select count(StudentNo) from dbo.Student  ";  
                    SqlCommand  du=new SqlCommand(re,tuy);  
               int we=(int)du.ExecuteScalar();
               Console.WriteLine(we);
               return we;  
            }  
            catch(Exception ex)  
            {  
               Console.WriteLine(ex.Message);  
                return -1;  
            }  
            finally  
            {  
           tuy.Close();   
          Console.WriteLine("关闭语句");

          //Console.Read();
            }    
       }

             public void QW()
             {
                 SqlConnection TY = new SqlConnection(ent);
                 try
                 {
                     TY.Open();
                     StringBuilder sb = new StringBuilder();
                     sb.AppendLine("select");
                     sb.AppendLine("      StudentNo");
                     sb.AppendLine("        ,StudentName");
                     sb.AppendLine("from");
                     sb.AppendLine("dbo.Student");

                     SqlCommand com = new SqlCommand(sb.ToString(), TY);
                     SqlDataReader reader = com.ExecuteReader();
                     Console.WriteLine("--------------------");
                     Console.WriteLine("学号\t 姓名");
                     Console.WriteLine("--------------------");

                     StringBuilder sbl = new StringBuilder();

                    
                     while (reader.Read())
                     {
                         sbl.AppendFormat("{0}\t{1}", reader["StudentNo"], reader["StudentName"]);
                         Console.WriteLine(sbl);
                         sbl.Length = 0;
                     }
                     Console.WriteLine("--------------------");
                     reader.Close();
                 }
                 catch (Exception)
                 {
                     Console.WriteLine("数据库操作失败");
                 }
                 finally
                 {
                     TY.Close();
                     //Console.Read();
                 }
                 
                
             }

        //上机练习一
             public string fel(string stuno)
             {
                 SqlConnection up = new SqlConnection(ent);
                 try
                 {
                     up.Open();
                     StringBuilder sa = new StringBuilder();
                     sa.AppendLine("select");
                     sa.AppendLine("      StudentNo");
                     sa.AppendLine("       ,StudentName ");
                     sa.AppendLine("from");
                     sa.AppendLine("       dbo.Student ");
                     sa.AppendLine("where");
                     sa.AppendLine("       StudentNo=" + stuno);

                     SqlCommand fd = new SqlCommand(sa.ToString(), up);
                     SqlDataReader rader = fd.ExecuteReader();
                     string name = string.Empty;
                     if (rader.Read())
                     {
                         name = Convert.ToString(rader["StudentName"]);
                     }
                     rader.Close();
                     return name;
                 }
                 catch (Exception)
                 {
                     return string.Empty;
                 }
                 finally
                 {
                     up.Close();
                 }
             }
      
             public void show()
             {
                 Console.WriteLine("请输入学生号:");
                 string stuno = Console.ReadLine();               
                 string name=fel(stuno);

                 if (name.Equals(string.Empty))
                 {
                     Console.WriteLine("出现异常!");
                 }
                 else
                 {
                     StringBuilder sb = new StringBuilder();
                     sb.AppendFormat("学号是{0}的学生姓名为:{1}",stuno,name);
                     Console.WriteLine(sb);
                     
                 }
             }

        /////////////////////////////////////////////////
       //上机练习二
             public SqlDataReader Get(string stuName)
             {
                 try
                 {
                     SqlConnection conn = new SqlConnection(ent);
                     conn.Open();

                     StringBuilder sd = new StringBuilder();
                     //这是一个包含模糊查询和链接查询的SQL语句
                     sd.AppendLine(" select ");
                     sd.AppendLine("      A.StudentNo ");
                     sd.AppendLine("      ,A.StudentName ");
                     sd.AppendLine("      ,A.Sex ");
                     sd.AppendLine("      ,B.GradeName ");
                     sd.AppendLine("      ,A.Phone ");
                     sd.AppendLine("      ,A.Address ");
                     sd.AppendLine("      ,A.BornDate ");
                     sd.AppendLine("      ,A.Email ");
                     sd.AppendLine(" from ");
                     sd.AppendLine("   dbo.Student as A,dbo.Grade as B");
                     sd.AppendLine(" where ");
                     sd.AppendLine("      StudentName like '%" + stuName + "%'");
                     sd.AppendLine(" and ");
                     sd.AppendLine("      A.GradeId=B.GradeId");
                     SqlCommand com = new SqlCommand(sd.ToString(), conn);

                 }
                 catch(Exception)
                 {
                     return null;
                 }
             }

             public void Byname()
             {
                 StringBuilder sc = new StringBuilder();
                 Console.WriteLine("请输入学生姓名:");
                 string stuNo = Console.ReadLine();
                 SqlDataReader reder = Get(stuNo);
                 if(reder==null)
                 {
                     Console.WriteLine("出现异常!");
                     return;
                 }
                 Console.WriteLine("-------------------------------");
                 Console.WriteLine("学号\t姓名\t性别\t年级\t联系电话\t地址\t\t出生日期\t\t邮箱");
                 while(reder.Read())
                 {
                     sc.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}",
                                     reder["StudentNo"],reder["StudentName"],reder["Sex"],
                                     reder["GradeName"],reder["Phone"],reder["Address"],
                                     reder["BornDate"],reder["Email"]);
                     Console.WriteLine(sc.ToString());
                     sc.Length = 0;                                  
                 }
                 Console.WriteLine("-------------------------------");
                 reder.Close();

             }

        
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值