欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#+Sql Server开发一个简易的学生管理系统

程序员文章站 2022-05-28 10:02:48
...

效果图
C#+Sql Server开发一个简易的学生管理系统
使用VS2017新建一个控制台应用程序,连接数据库,即可对数据库进行增删改查,更多的功能还没有写,不适合直接拿去做课设,不过基本架子都有了,更多复杂的功能写sql就行了。
代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;                  //表的命名空间
using System.Data.SqlClient;        //和sql相关的声明

namespace ConnectionSQLServerDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            while(true)
            {
                Console.WriteLine("请输入你要进行的操作:");
                Console.WriteLine("1.查找所有学生的信息.");
                Console.WriteLine("2.查找指定学号学生的信息.");
                Console.WriteLine("3.添加一个学生的信息.");
                Console.WriteLine("4.修改学生的信息.");
                Console.WriteLine("5.删除学生.");
                Console.WriteLine("0.退出系统.");
                string handleKey = Console.ReadLine();
                if (handleKey == "1")
                {
                    //根据学号查找学生的详细信息
                    queryStuInfo();
                }
                else if (handleKey == "0")
                {
                    System.Environment.Exit(0);
                }
                else if(handleKey == "2")
                {
                    queryAllStusInfo();
                }
                else if(handleKey == "3")
                {
                    insertStu2DB0920();
                }
                else if(handleKey == "4")
                {
                    updataStuInfo();
                }
                else if(handleKey == "5")
                {
                    deleteStuInfo();
                }
            }
        }
        public static void deleteStuInfo()
        {
            SqlConnection sc = null;
            try
            {
                //管理SqlConnection
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                //设置连接的基本设置
                scsb.DataSource = "DESKTOP-04PLOJK\\SQLEXPRESS";
                scsb.UserID = "sa";
                scsb.Password = "root";
                scsb.InitialCatalog = "DB0920";
                sc = new SqlConnection(scsb.ToString());
                //创建连接
                if (sc.State == ConnectionState.Closed)
                {
                    sc.Open();
                }

                Console.WriteLine("请输入待删除学生的学号.");
                string stuNo = Console.ReadLine();
                bool flag = checkStuExist(stuNo);
                if (!flag)
                {
                    Console.WriteLine("您删除的学生不存在,继续请按5,退出请按0");
                    stuNo = Console.ReadLine();
                    if (stuNo == "0")
                    {
                        System.Environment.Exit(0);
                    }
                    deleteStuInfo();
                }
                else
                {
                    string strSql = "delete students where Sno = '"+stuNo+"'";
                    //执行sql
                    SqlCommand command = new SqlCommand(strSql, sc);
                    SqlDataReader sdr = command.ExecuteReader();
                    Console.WriteLine("删除学生成功.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                sc.Close();
            }
        }
        public static void updataStuInfo()
        {
            SqlConnection sc = null;
            try
            {
                //管理SqlConnection
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                //设置连接的基本设置
                scsb.DataSource = "DESKTOP-04PLOJK\\SQLEXPRESS";
                scsb.UserID = "sa";
                scsb.Password = "root";
                scsb.InitialCatalog = "DB0920";
                sc = new SqlConnection(scsb.ToString());
                //创建连接
                if (sc.State == ConnectionState.Closed)
                {
                    sc.Open();
                }
                
                Console.WriteLine("请输入待修改学生的学号.");
                string stuNo = Console.ReadLine();
                bool flag = checkStuExist(stuNo);
                if (!flag)
                {
                    Console.WriteLine("您修改的学生不存在,继续请按4,退出请按0");
                    stuNo = Console.ReadLine();
                    if (stuNo == "0")
                    {
                        System.Environment.Exit(0);
                    }
                    updataStuInfo();
                }
                else
                {
                    Console.WriteLine("请输入姓名:");
                    string sName = Console.ReadLine();
                    Console.WriteLine("请输入性别:");
                    string sSex = Console.ReadLine();
                    Console.WriteLine("请输入地址:");
                    string sAddre = Console.ReadLine();
                    Console.WriteLine("请输入电话:");
                    string sPhone = Console.ReadLine();
                    Console.WriteLine("请输入年龄:");
                    int sAge = int.Parse(Console.ReadLine());

                    string strSql = "update students set Sname='"+sName+"',Ssex='"+sSex+"',Saddress='"+sAddre+"',Sphone='"+sPhone+"',Sage='"+sAge+"' where Sno='"+stuNo+"'";

                    //执行sql
                    SqlCommand command = new SqlCommand(strSql, sc);
                    SqlDataReader sdr = command.ExecuteReader();
                    Console.WriteLine("修改学生信息成功.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                sc.Close();
            }
        }
        public static void insertStu2DB0920()
        {
            SqlConnection sc = null;
            try
            {
                //管理SqlConnection
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                //设置连接的基本设置
                scsb.DataSource = "DESKTOP-04PLOJK\\SQLEXPRESS";
                scsb.UserID = "sa";
                scsb.Password = "root";
                scsb.InitialCatalog = "DB0920";
                sc = new SqlConnection(scsb.ToString());
                //创建连接
                if (sc.State == ConnectionState.Closed)
                {
                    sc.Open();
                }

                Console.WriteLine("请输入学号:");
                string stuNo = Console.ReadLine();
                bool flag = checkStuExist(stuNo);
                if(flag)
                {
                    Console.WriteLine("您插入的学生信息已存在,请重新输入...退出请按0");
                    stuNo = Console.ReadLine();
                    if (stuNo == "0")
                    {
                        System.Environment.Exit(0);
                    }
                    insertStu2DB0920();
                }
                else
                {
                    Console.WriteLine("请输入姓名:");
                    string sName = Console.ReadLine();
                    Console.WriteLine("请输入性别:");
                    string sSex = Console.ReadLine();
                    Console.WriteLine("请输入地址:");
                    string sAddre = Console.ReadLine();
                    Console.WriteLine("请输入电话:");
                    string sPhone = Console.ReadLine();
                    Console.WriteLine("请输入年龄:");
                    int sAge = int.Parse(Console.ReadLine());

                    string strSql = "insert into students(sno, Sname, Ssex, Saddress, Sphone, Sage) values ('" + stuNo + "', '" + sName + "', '" + sSex + "', '" + sAddre + "', '+" + sPhone + "+', " + sAge + ")";

                    //执行sql
                    SqlCommand command = new SqlCommand(strSql, sc);
                    SqlDataReader sdr = command.ExecuteReader();
                    Console.WriteLine("成功插入一条学生信息.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                sc.Close();
            }
        }
        //判断学生是否存在
        public static bool checkStuExist(string stuNo)
        {
            SqlConnection sc = null;
            try
            {
                //管理SqlConnection
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                //设置连接的基本设置
                scsb.DataSource = "DESKTOP-04PLOJK\\SQLEXPRESS";
                scsb.UserID = "sa";
                scsb.Password = "root";
                scsb.InitialCatalog = "DB0920";
                sc = new SqlConnection(scsb.ToString());
                //创建连接
                if (sc.State == ConnectionState.Closed)
                {
                    sc.Open();
                }
                string strSql = "select * from students where Sno = '" + stuNo + "'";

                //执行sql
                SqlCommand command = new SqlCommand(strSql, sc);
                SqlDataReader sdr = command.ExecuteReader();
                bool flag = sdr.Read();
                return flag;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                sc.Close();
            }
            return false;
        }
        public static void queryAllStusInfo()
        {
            SqlConnection sc = null;
            try
            {
                //管理SqlConnection
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                //设置连接的基本设置
                scsb.DataSource = "DESKTOP-04PLOJK\\SQLEXPRESS";
                scsb.UserID = "sa";
                scsb.Password = "root";
                scsb.InitialCatalog = "DB0920";
                sc = new SqlConnection(scsb.ToString());
                //创建连接
                if (sc.State == ConnectionState.Closed)
                {
                    sc.Open();
                }

                Console.WriteLine("请输入您要查询的学号:");
                string stuNo = Console.ReadLine();
                string strSql = "select * from students where Sno = '" + stuNo + "'";

                //执行sql
                SqlCommand command = new SqlCommand(strSql, sc);
                SqlDataReader sdr = command.ExecuteReader();
                while (sdr.Read())
                {
                    Console.Write("sno=" + sdr["sno"].ToString());
                    Console.Write(",sname=" + sdr["sname"].ToString());
                    Console.Write(",ssex=" + sdr["ssex"].ToString());
                    Console.Write(",saddress=" + sdr["saddress"].ToString());
                    Console.Write(",sphone=" + sdr["sphone"].ToString());
                    Console.WriteLine(",sage=" + sdr["sage"].ToString() + "\n\n\n\n");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                sc.Close();
            }
        }
        public static void queryStuInfo()
        {
            SqlConnection sc = null;
            try
            {
                //管理SqlConnection
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                //设置连接的基本设置
                scsb.DataSource = "DESKTOP-04PLOJK\\SQLEXPRESS";
                scsb.UserID = "sa";
                scsb.Password = "root";
                scsb.InitialCatalog = "DB0920";
                sc = new SqlConnection(scsb.ToString());
                //创建连接
                if (sc.State == ConnectionState.Closed)
                {
                    sc.Open();
                }
                string strSql = "select * from students";

                //执行sql
                SqlCommand command = new SqlCommand(strSql, sc);
                SqlDataReader sdr = command.ExecuteReader();
                while (sdr.Read())
                {
                    Console.Write("sno=" + sdr["sno"].ToString());
                    Console.Write(",sname=" + sdr["sname"].ToString());
                    Console.Write(",ssex=" + sdr["ssex"].ToString());
                    Console.Write(",saddress=" + sdr["saddress"].ToString());
                    Console.Write(",sphone=" + sdr["sphone"].ToString());
                    Console.WriteLine(",sage=" + sdr["sage"].ToString());
                }
                Console.Write("\n\n\n\n");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                sc.Close();
            }
        }
    }
}

相关标签: C#桌面程序开发