c# sql 数据库实验大作业 (学生信息管理)

视频讲解链接:https://www.bilibili.com/video/BV1FA411B74H/
一 层级概括
主要界面包括学生登录界面,管理员登录界面,注册界面,学生表,课程表,SC表的查看,
以及增删改查界面
二主要代码和界面演示
1.注册界面
c# sql 数据库实验大作业 (学生信息管理)_第1张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test518
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (comboBox1.Text.Trim() == "学生")//如果是学生注册,添加到SysUsers
            {
                try
                {
                    string connString = "Data Source=.;Initial Catalog=School;User ID=sa;Password=lrx359641708";//数据库连接字符串
                    SqlConnection connection = new SqlConnection(connString);//创建connection对象
                    string sql = "insert into SysUser (UserID,   UserPassWord ,   UserSchoolID, UserMobile, UserBirthday , UserIdentity  ) " +
                                                                     "values (@userid, @userpassword,@userschoolid,@usermobile,@userbirthday,@useridentity)";
                    SqlCommand command = new SqlCommand(sql, connection);
                    SqlParameter sqlParameter = new SqlParameter("@userid", textBox1.Text);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@userpassword", textBox2.Text);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@userschoolid", textBox3.Text);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@usermobile", textBox4.Text);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@userbirthday", dateTimePicker1.Value);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@useridentity", comboBox1.Text);
                    command.Parameters.Add(sqlParameter);
                    
                    //打开数据库连接
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                    MessageBox.Show("注册成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (comboBox1.Text.Trim() == "管理员")//如果是管理员注册,添加到SyUsers
            {
                try
                {
                    string connString = "Data Source=.;Initial Catalog=School;User ID=sa;Password=lrx359641708";//数据库连接字符串
                    SqlConnection connection = new SqlConnection(connString);//创建connection对象
                    string sql = "insert into SyUser (UserID,   UserPassWord ,   UserSchoolID, UserMobile, UserBirthday , UserIdentity  ) " +
                                                                        "values (@userid, @userpassword,@userschoolid,@usermobile,@userbirthday,@useridentity)";
                    SqlCommand command = new SqlCommand(sql, connection);
                    SqlParameter sqlParameter = new SqlParameter("@userid", textBox1.Text);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@userpassword", textBox2.Text);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@userschoolid", textBox3.Text);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@usermobile", textBox4.Text);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@userbirthday", dateTimePicker1.Value);
                    command.Parameters.Add(sqlParameter);
                    sqlParameter = new SqlParameter("@useridentity", comboBox1.Text);
                    command.Parameters.Add(sqlParameter);
                   
                    //打开数据库连接
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                    MessageBox.Show("注册成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            this.Close();//界面关闭

        }



        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public Byte[] mybyte = new byte[0];

      

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");

                if (regex.IsMatch(textBox1.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入密码格式正确!");
                }
                else
                {
                    MessageBox.Show("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
                    textBox1.Focus();
                }
            }
            else
            {
                MessageBox.Show("Please fill in the full information!");
            }

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Register_Load(object sender, EventArgs e)
        {

        }
    }
}

2.学生登录界面
c# sql 数据库实验大作业 (学生信息管理)_第2张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test518
{
    public partial class StudentLogin : Form
    {
        public StudentLogin()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text.Trim();  //取出账号
            string password = textBox2.Text.Trim();  //取出密码并加密

     

            //string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); //读取连接字符串
            string myConnString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708";

            SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接对象
            sqlConnection.Open();

            string sql = "select UserID,UserPassword from SysUser where UserID = '" + username + "' and UserPassword = '" + password + "'";                                            //编写SQL命令
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);

            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

            if (sqlDataReader.HasRows && textBox3.Text == code)
            {
                MessageBox.Show("欢迎使用!");             //登录成功
                stumain stumain= new stumain();
              stumain.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("登录失败!");
                return;
            }
            sqlDataReader.Close();
          //  sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')";                                            //编写SQL命令
         //   sqlCommand = new SqlCommand(sql, sqlConnection);
           // sqlCommand.ExecuteNonQuery();
            //sqlConnection.Close();
        }

      

        public string code;        

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Register register = new Register();
            register.ShowDialog();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            //随机实例化 
            Random ran = new Random();
            int number;
            char code1;
            //取五个数 
            for (int i = 0; i < 5; i++)
            {
                number = ran.Next();
                if (number % 2 == 0)
                    code1 = (char)('0' + (char)(number % 10));
                else
                    code1 = (char)('A' + (char)(number % 26)); //转化为字符 

                this.code += code1.ToString();
            }

            label5.Text = code;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            glyLogin glyLogin = new glyLogin();
            glyLogin.Show();
         
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}

3.管理员登录界面
c# sql 数据库实验大作业 (学生信息管理)_第3张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test518
{
    public partial class glyLogin : Form
    {
        public glyLogin()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text.Trim();  //取出账号
            string password = textBox2.Text.Trim();  //取出密码并加密



            //string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); //读取连接字符串
            string myConnString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708";

            SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接对象
            sqlConnection.Open();

            string sql = "select UserID,UserPassword from SyUser where UserID = '" + username + "' and UserPassword = '" + password + "'";                                            //编写SQL命令
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);

            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

            if (sqlDataReader.HasRows )
            {
                MessageBox.Show("欢迎使用!");             //登录成功
                glymain glymain = new glymain();                                      //Main form2 = new Main();
                glymain.Show();                                     // form2.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("登录失败!");
                return;
            }
            sqlDataReader.Close();
            //  sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')";                                            //编写SQL命令
            //   sqlCommand = new SqlCommand(sql, sqlConnection);
            // sqlCommand.ExecuteNonQuery();
            //sqlConnection.Close();
        }


       

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Register register = new Register();
            register.ShowDialog();
        }

        private void glyLogin_Load(object sender, EventArgs e)
        {

        }
    }
}

4.学生系统主要界面
c# sql 数据库实验大作业 (学生信息管理)_第4张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test518
{
    public partial class stumain : Form
    {
        public stumain()
        {
            InitializeComponent();
        }

        private void stumain_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“schoolDataSet3.Student”中。您可以根据需要移动或删除它。
          //  this.studentTableAdapter.Fill(this.schoolDataSet3.Student);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            form3.ShowDialog();
        }
    }
}

5.学生查看学生表
c# sql 数据库实验大作业 (学生信息管理)_第5张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test518
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“schoolDataSet13.Student”中。您可以根据需要移动或删除它。
            this.studentTableAdapter1.Fill(this.schoolDataSet13.Student);
        
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

6.学生查看课程表
c# sql 数据库实验大作业 (学生信息管理)_第6张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test518
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“schoolDataSet9.Course”中。您可以根据需要移动或删除它。
            this.courseTableAdapter.Fill(this.schoolDataSet9.Course);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

7.学生查看成绩
c# sql 数据库实验大作业 (学生信息管理)_第7张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test518
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“schoolDataSet10.SC”中。您可以根据需要移动或删除它。
            this.sCTableAdapter.Fill(this.schoolDataSet10.SC);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

8.管理员对学生信息的增删改查
c# sql 数据库实验大作业 (学生信息管理)_第8张图片

  private void button1_Click(object sender, EventArgs e)
        {
            String StuID = textBox1.Text.Trim();
            String StuName = textBox2.Text.Trim();
            String StuSex = textBox3.Text.Trim();
            String Stuage = textBox4.Text.Trim();
            String StuSdept = textBox5.Text.Trim();//取出字符串
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708");
            try
            {
                con.Open();
                String insertstr = "INSERT INTO  Student (Sno,Sname,Ssex,Sdept,Sage)    " + "VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuSdept + "'," + Stuage + ")";
                SqlCommand cmd = new SqlCommand(insertstr, con); //实例化数据库命令对象
                cmd.ExecuteNonQuery(); //执行命令
            }
            catch
            {
                MessageBox.Show("输入数据有误,请输入有效数据!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                con.Dispose(); //释放
            }
            this.studentTableAdapter4.Fill(this.schoolDataSet15.Student);//使其显示更新后的表

        }

private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708");
            try
            {

                con.Open();
                string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string delete_by_id = "delete from Student where Sno=" +  select_id ;
                SqlCommand cmd = new SqlCommand(delete_by_id, con);
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("请正确选择行!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                con.Dispose();
            }
            this.studentTableAdapter4.Fill(this.schoolDataSet15.Student);// 使其显示更新后的表

        }
改
 



 private void button3_Click(object sender, EventArgs e)
        {
            String StuID = "";
            if (textBox1.Text == "")
            {
                StuID = dataGridView1.SelectedCells[0].Value.ToString();

            }   //选中学号修改 }
            else StuID = textBox1.Text.Trim();//输入学号修改
            String StuName = textBox2.Text.Trim();
            String StuSex = textBox3.Text.Trim();
            String StuAge = textBox4.Text.Trim();
            String StuSdept = textBox5.Text.Trim();
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708");
            try
            {
                string insertStr = "";
                string insertStr1 = "";
                string insertStr2 = "";
                string insertStr3 = "";
                con.Open();
                if (StuName != "")
                {
                    insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";    //修改名字
                    SqlCommand cmd = new SqlCommand(insertStr, con);
                    cmd.ExecuteNonQuery();
                }

                if (StuSex != "")
                {
                    insertStr1 = "UPDATE Student SET Ssex = '" + StuSex + "' WHERE Sno = '" + StuID + "'";    //修改性别
                    SqlCommand cmd1 = new SqlCommand(insertStr1, con);
                    cmd1.ExecuteNonQuery();
                }
                if (StuSdept != "")
                {
                    insertStr2 = "UPDATE Student SET Sdept = '" + StuSdept + "' WHERE Sno = '" + StuID + "'";    //修改专业
                    SqlCommand cmd2 = new SqlCommand(insertStr2, con);
                    cmd2.ExecuteNonQuery();
                }
                if (StuAge != "")
                {
                    insertStr3 = "UPDATE Student SET Sage = '" + StuAge + "' WHERE Sno = '" + StuID + "'";    //修改年龄
                    SqlCommand cmd3 = new SqlCommand(insertStr3, con);
                    cmd3.ExecuteNonQuery();
                }
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                con.Dispose();
            }
            this.studentTableAdapter4.Fill(this.schoolDataSet15.Student);
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }

  private void button4_Click(object sender, EventArgs e)
        {
            String StuID = textBox1.Text.Trim();
            String StuName = textBox2.Text.Trim();
            String StuSex = textBox3.Text.Trim();
            String StuAge = textBox4.Text.Trim();
            String StuSdept = textBox5.Text.Trim();
            String conn = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708";
            SqlConnection sqlConnection = new SqlConnection(conn);  //实例化连接对象
            try
            {
                String select_by_id = "select * from Student where ";
                int flag = 0;  //0是单条件,1是多条件
                sqlConnection.Open();
                if (StuID != "")
                    select_by_id += "Sno='" + StuID + "'";    //单条件 按学号
                if (StuName != "")
                {
                    if (flag == 0)
                    {
                        select_by_id += "Sname LIKE'" + StuName + "'";
                        flag = 1;
                    }
                    if (flag == 1)
                        select_by_id += "And Sname LIKE'" + StuName + "'";
                }
                if (StuAge != "")
                {
                    if (flag == 0)
                    {
                        select_by_id += "Sage='" + StuAge + "'";
                        flag = 1;
                    }
                    if (flag == 1)
                        select_by_id += "And Sage='" + StuAge + "'";
                }
                if (StuSdept != "")
                {
                    if (flag == 0)
                    {
                        select_by_id += "Sdept='" + StuSdept + "'";
                        flag = 1;
                    }
                    if (flag == 1)
                        select_by_id += "And Sdept='" + StuSdept + "'";
                }
                if (StuSex != "")
                {
                    if (flag == 0)
                    {
                        select_by_id += "Ssex='" + StuSex + "'";
                        flag = 1;
                    }
                    if (flag == 1)
                        select_by_id += "And Ssex='" + StuSex + "'";
                }
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句!");
            }
            finally
            {
                sqlConnection.Close();
            }

        }

返回

 private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            String conn = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708";
            SqlConnection sqlConnection = new SqlConnection(conn);  //实例化连接对象
            try
            {
                String select_by_id = "select * from Student";

                sqlConnection.Open();
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句!");
            }
            finally
            {
                sqlConnection.Close();
            }

        }

9.管理员对课程信息的增删改查
c# sql 数据库实验大作业 (学生信息管理)_第9张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test518
{
    public partial class COURD : Form
    {
        public COURD()
        {
            InitializeComponent();
        }

        private void COURD_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“schoolDataSet6.Course”中。您可以根据需要移动或删除它。
            this.courseTableAdapter.Fill(this.schoolDataSet6.Course);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String Cno = textBox1.Text.Trim();
            String Cname = textBox2.Text.Trim();
            String Cpno = textBox3.Text.Trim();
            String Ccredit = textBox4.Text.Trim();                 //读取需要插入的值
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708");
            try
            {
                con.Open();
                string insertStr = "INSERT INTO  Course (Cno,Cname,Cpno,Ccredit)    " +              //拼接字符串
                    "VALUES ('" + Cno + "','" + Cname + "','" + Cpno + "','" + Ccredit + "')";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                con.Dispose();
            }
            this.courseTableAdapter.Fill(this.schoolDataSet6.Course);
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708");     //定义
            try
            {
                con.Open();    //打开
                string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
                string delete_by_id = "delete from Course where Cno=" + select_id;//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_id, con);        //使用
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("请正确选择行!");
            }
            finally
            {
                con.Dispose();     //释放
            }
            this.courseTableAdapter.Fill(this.schoolDataSet6.Course);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String Cno = "";
            if (textBox1.Text == "")
            { Cno = dataGridView1.SelectedCells[0].Value.ToString(); }
            else Cno = textBox1.Text.Trim();

            String Cname = textBox2.Text.Trim();
            String Cpno = textBox3.Text.Trim();
            String Ccredit = textBox4.Text.Trim();
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708");

            try
            {
                string insertStr = "";
                string insertStr1 = "";
                string insertStr3 = "";
                con.Open();
                if (Cname != "")
                {
                    insertStr = "UPDATE Course SET Cname = '" + Cname + "' WHERE Cno = '" + Cno + "'";    //修改名字
                    SqlCommand cmd = new SqlCommand(insertStr, con);
                    cmd.ExecuteNonQuery();
                }

                if (Cpno != "")
                {
                    insertStr1 = "UPDATE Course SET Cpno = '" + Cpno + "' WHERE Cno = '" + Cno + "'";    //修改先导
                    SqlCommand cmd1 = new SqlCommand(insertStr1, con);
                    cmd1.ExecuteNonQuery();
                }

                if (Ccredit != "")
                {
                    insertStr3 = "UPDATE Course SET Ccredit = '" + Ccredit + "' WHERE Cno = '" + Cno + "'";    //修改学分
                    SqlCommand cmd3 = new SqlCommand(insertStr3, con);
                    cmd3.ExecuteNonQuery();
                }

            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                con.Dispose();
            }
            this.courseTableAdapter.Fill(this.schoolDataSet6.Course);
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";

        }
    }
}

10.管理员对学生选课成绩增删改查
c# sql 数据库实验大作业 (学生信息管理)_第10张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test518
{
    public partial class SCURD : Form
    {
        public SCURD()
        {
            InitializeComponent();
        }

        internal static void showdialog()
        {
            //      throw new NotImplementedException();
        }

        private void SCURD_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“schoolDataSet5.SC”中。您可以根据需要移动或删除它。
            this.sCTableAdapter.Fill(this.schoolDataSet5.SC);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String StuID = textBox1.Text.Trim();
            String StuCno = textBox2.Text.Trim();
            String StuGrade = textBox3.Text.Trim();
            //读取需要插入的值
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708");
            try
            {
                con.Open();
                string insertStr = "INSERT INTO  SC (Sno,Cno,Grade)    " +             "VALUES ('" + StuID + "','" + StuCno + "','" + StuGrade + "')";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                con.Dispose();
            }
            this.sCTableAdapter.Fill(this.schoolDataSet5.SC);
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708");     //定义
            try
            {
                con.Open();    //打开
                string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
                string select_cno = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//选择的当前行第2列的值,也就是cno
                string delete_by_id = "delete from SC where Sno=" + select_id + "AND Cno=" + select_cno;//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_id, con);        //使用
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("请正确选择行!");
            }
            finally
            {
                con.Dispose();     //释放
            }
            this.sCTableAdapter.Fill(this.schoolDataSet5.SC);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            String StuID = "";
            String StuGrade = "";
            String StuCno = "";
            if (textBox1.Text == "")
            { StuID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); }
            else StuID = textBox1.Text.Trim();//输入学号修改
            if (textBox2.Text == "")
            {
                StuCno = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            } //选中行修改          
            else StuCno = textBox2.Text.Trim();//输入修改
            if (textBox3.Text == "")
            {
                StuGrade = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            }//选中行修改
            else StuGrade = textBox3.Text.Trim();//输入修改
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708");

            try
            {
                string insertStr = "";
                string insertStr1 = "";
                con.Open();
                if (StuCno != "")
                {
                    insertStr = "UPDATE SC SET Cno = '" + StuCno + "' WHERE Sno = '" + StuID + "'" + "AND Grade='" + StuGrade + "'";    //修改课程
                    SqlCommand cmd = new SqlCommand(insertStr, con);
                    cmd.ExecuteNonQuery();
                }
                if (StuGrade != "")
                {
                    insertStr1 = "UPDATE SC SET Grade = '" + StuGrade + "' WHERE Sno = '" + StuID + "'" + "AND Cno='" + StuCno + "'"; ;    //修改成绩
                    SqlCommand cmd1 = new SqlCommand(insertStr1, con);
                    cmd1.ExecuteNonQuery();
                }
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                con.Close();
            }
            this.sCTableAdapter.Fill(this.schoolDataSet5.SC);
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";

        }

        private void button4_Click(object sender, EventArgs e)
        {
            String StuID = textBox1.Text.Trim();
            String StuCno = textBox2.Text.Trim();
            String StuGrade = textBox3.Text.Trim();
            String conn = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708";
            SqlConnection sqlConnection = new SqlConnection(conn);  //实例化连接对象
            try
            {
                String select_by_id = "select * from SC where ";
                int flag = 0;  //0是单条件,1是多条件
                sqlConnection.Open();
                if (StuID != "")
                {
                    select_by_id += "Sno='" + StuID + "'"; flag = 1;
                } //单条件 按学号
                if (StuCno != "")
                {
                    if (flag == 0)
                    {
                        select_by_id += "Cno='" + StuCno + "'" + " ORDER BY Grade DESC";//单条件按课程号
                        flag = 1;
                    }
                    else if (flag == 1)//多条件按学号+课程号
                    { select_by_id += "And Cno='" + StuCno + "'"; }

                }
                if (flag == 0 && StuGrade != "")//单条件,查询平均分高于输入的学号
                {
                    select_by_id = "select Sno,AVG(Grade) AS Avg FROM SC" + " GROUP BY Sno HAVING AVG(Grade)>=" + "'" + StuGrade + "'";
                }
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句!");
            }
            finally
            {
                sqlConnection.Close();
            }
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";

        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";

            String conn = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=lrx359641708";
            SqlConnection sqlConnection = new SqlConnection(conn);  //实例化连接对象
            try
            {
                String select_by_id = "select * from SC";

                sqlConnection.Open();
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句!");
            }
            finally
            {
                sqlConnection.Close();

            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}

我的数据库大作业终于告一段落
不得不说相比于以往的语言类课程我更喜欢数据库的学习,可能是懒惰,或许是天生对代码不感冒,不太擅长编程的我也愿意做一做数据库大作业,虽然我知道和同一起跑线的同学的成果与我的小程序判若云泥,自己动手做出一点东西已经心满意足

你可能感兴趣的:(c# sql 数据库实验大作业 (学生信息管理))