数据库大作业,代码展示

在这里插入图片描述
登录界面代码:

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 Form1 : Form
{
    public class Class1
    {
        public static string UserID;
    }
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string username = textBox1.Text.Trim();
        string password = textBox2.Text.Trim();
        string myConnString = "Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123";

        SqlConnection sqlConnection = new SqlConnection(myConnString);
        sqlConnection.Open();

        string sql = "select 用户名称,密码 from UserTable where 用户名称 ='" + username + "' and 密码 ='" + password + "'";

        SqlCommand sqlcommand = new SqlCommand(sql, sqlConnection);

        SqlDataReader sqlDataReader = sqlcommand.ExecuteReader();
        if (sqlDataReader.HasRows&&textBox3.Text==label5.Text)
        {
            if(username=="admin")
            {
                Class1.UserID = username;
                MessageBox.Show("欢迎使用!");
                Manager manager = new Manager();
                manager.Show();
                this.Hide();
            }
            else
            {
                Class1.UserID = username;
                MessageBox.Show("欢迎使用!");
                Form2 form2 = new Form2();
                form2.Show();
                this.Hide();

            }

        }
        else
        {
            MessageBox.Show("登录失败,输入密码或验证码错误!");
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            return;
        }
        sqlDataReader.Close();
        sqlConnection.Close();

    }

    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
    public static string EncryptWithMD5(string source)
    {
        byte[] sor = Encoding.UTF8.GetBytes(source);
        MD5 md5 = MD5.Create();
        byte[] result = md5.ComputeHash(sor);
        StringBuilder strbul = new StringBuilder(40);
        for (int i = 0; i < result.Length; i++)
        {
            strbul.Append(result[i].ToString("x2"));
        }
        return strbul.ToString();
    }
    public string code;
    private void Form1_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 label5_Click(object sender, EventArgs e)
    {
        
    }

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

    private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        Form3 form3 = new Form3();
        form3.Show();
        this.Hide();
    }
}
}

在这里插入图片描述

注册窗口代码:

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 Register_Load(object sender, EventArgs e)
    {

    }
    public static string EncryptWithMD5(string source)
    {
        byte[] sor = Encoding.UTF8.GetBytes(source);
        MD5 md5 = MD5.Create();
        byte[] result = md5.ComputeHash(sor);
        StringBuilder strbul = new StringBuilder(40);
        for (int i = 0; i < result.Length; i++)
        {
            strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
        }
        return strbul.ToString();
    }

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

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string connString = "Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123";
            SqlConnection connection = new SqlConnection(connString);
            string sql = "insert into UserTable (用户名称,密码,学号,手机号码,出生日期)" +
                                                    "values (@userid, @userpassword,@userschoolid,@usermobile,@userbirthday)";
            SqlCommand command = new SqlCommand(sql, connection);

            SqlParameter sqlParameter = new SqlParameter("@userid", textBox1.Text);
            command.Parameters.Add(sqlParameter);
            sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(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);

            //打开数据库连接
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
            MessageBox.Show("register succeed");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        this.Close();
    }
    private void textBox1_Leave(object sender, EventArgs e)
    {
        if (textBox1.Text.Trim() != "")
        {
            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("请填写全部信息!");
        }
    }
}
}

在这里插入图片描述

找回密码窗口代码:

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.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test518
{
public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
    }
    public string code;
    public Byte[] mybyte = new byte[0];
    public static string EncryptWithMD5(string source)
    {
        byte[] sor = Encoding.UTF8.GetBytes(source);
        MD5 md5 = MD5.Create();
        byte[] result = md5.ComputeHash(sor);
        StringBuilder strbul = new StringBuilder(40);
        for (int i = 0; i < result.Length; i++)
        {
            strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
        }
        return strbul.ToString();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        string username = textBox1.Text.Trim();
        string cellphone = textBox2.Text.Trim();
        int flag1 = 0;
        int flag2 = 0;

        string myConnString = "Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123";

        SqlConnection sqlConnection = new SqlConnection(myConnString);
        sqlConnection.Open();

        string sql1 = "select 用户名称,手机号码 from UserTable where 用户名称 = '" + username + "' and 手机号码 = '" + cellphone + "'";                                            
        SqlCommand sqlCommand1 = new SqlCommand(sql1, sqlConnection);
    
        SqlDataReader sqlDataReader1 = sqlCommand1.ExecuteReader();
        if (sqlDataReader1.HasRows)
        {
            if (textBox3.Text.Trim() != "")
            {
                Regex regex = new Regex(@"(?=.*[0-9]).{3,15}");

                if (regex.IsMatch(textBox3.Text))
                {
                    flag1 = 1;
                }

            }
        }
            sqlDataReader1.Close();
        if (flag1 == 1)
        {
            string password = EncryptWithMD5(textBox3.Text.Trim());
            string sql3 = "update UserTable set 密码='" + password + "' where 用户名称='" + username + "'";
            SqlCommand sqlCommand3 = new SqlCommand(sql3, sqlConnection);
            SqlDataReader sqlDataReader3 = sqlCommand3.ExecuteReader();
            MessageBox.Show("修改成功");
            sqlDataReader3.Close();
            Form1 form1 = new Form1();
            form1.Show();
            this.Hide();
        }
        else
        {
            label5.Text = "输入密码错误或手机号码错误,请重新输入";
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            return;
        }
            sqlConnection.Close();
        }

    private void button2_Click(object sender, EventArgs e)
    {
        Form1 form1 = new Form1();
        form1.Show();
        this.Hide();
    }
}
}

在这里插入图片描述

管理员界面代码:

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 Manager : Form
{
    public Manager()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 form1 = new Form1();
        form1.Show();
        this.Hide();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        manageruser manageruser = new manageruser();
        manageruser.Show();
        this.Hide();
    }

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

    private void button4_Click(object sender, EventArgs e)
    {
        managercs managercs = new managercs();
        managercs.Show();
        this.Hide();
    }

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

在这里插入图片描述

学生信息界面代码:

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 manageruser : Form
{
    public manageruser()
    {
        InitializeComponent();
    }
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123");


    private void manageruser_Load(object sender, EventArgs e)
    {
        this.studentTableTableAdapter.Fill(this.sTUDENTDataSet2.StudentTable);


    }
    public static string EncryptWithMD5(string source)
    {
        byte[] sor = Encoding.UTF8.GetBytes(source);
        MD5 md5 = MD5.Create();
        byte[] result = md5.ComputeHash(sor);
        StringBuilder strbul = new StringBuilder(40);
        for (int i = 0; i < result.Length; i++)
        {
            strbul.Append(result[i].ToString("x2"));
        }
        return strbul.ToString();
    }

    private void button5_Click(object sender, EventArgs e)
    {
        Manager manager = new Manager();
        manager.Show();
        this.Hide();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            String Mno = textBox1.Text.Trim();
            String Mname = textBox2.Text.Trim();
            String Msex = textBox3.Text.Trim();
            String Mbirth = textBox4.Text.Trim();
            String Mzhuan = textBox5.Text.Trim();
            String Mdept = textBox6.Text.Trim();
            String Mcelllphone = textBox7.Text.Trim();
            String Mming = textBox8.Text.Trim();

            con.Open();
            string insertStr = "INSERT INTO StudentTable(学号,姓名,性别,出生日期,手机号码,民族,年级,专业) " +
                "VALUES('" + Mno + "','" + Mname + "','" + Msex + "','" + Mbirth + "','" + Mcelllphone + "','" + Mming + "','" + Mdept + "','" + Mzhuan + "')";
            SqlCommand cmd = new SqlCommand(insertStr, con);
            cmd.ExecuteNonQuery();
        }
        catch
        {
            MessageBox.Show("输入不符要求!");
        }
        finally
        {
            con.Dispose();
        }
       
        this.studentTableTableAdapter.Fill(this.sTUDENTDataSet2.StudentTable);
    }


    private void button2_Click(object sender, EventArgs e)
    {
        try
        {

            con.Open();
            string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string delete_by_id = "delete from StudentTable where 学号=" + select_id;
            SqlCommand cmd = new SqlCommand(delete_by_id, con);
            cmd.ExecuteNonQuery();
        }
        catch
        {
            MessageBox.Show("请正确选择行!");
        }
        finally
        {
            con.Dispose();
        }

        this.studentTableTableAdapter.Fill(this.sTUDENTDataSet2.StudentTable);
    }

    private void button3_Click_1(object sender, EventArgs e)
    {
        String Mno = textBox1.Text.Trim();
        String Mname = textBox2.Text.Trim();
        String Msex = textBox3.Text.Trim();
        String Mbirth = textBox4.Text.Trim();
        String Mzhuan = textBox5.Text.Trim();
        String Mdept = textBox6.Text.Trim();
        String Mcelllphone = textBox7.Text.Trim();
        String Mming = textBox8.Text.Trim();
        try
        {
            con.Open();
            if (Mname != "")
            {
                string insertStr = "UPDATE StudentTable SET 姓名 = '" + Mname + "' WHERE   学号='" + Mno + "'";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            if (Msex != "")
            {
                string insertStr = "UPDATE StudentTable SET 性别 = '" + Msex + "' WHERE   学号='" + Mno + "'";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            if (Mbirth != "")
            {
                string insertStr = "UPDATE StudentTable SET 出生日期 = '" + Mbirth + "' WHERE   学号='" + Mno + "'";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            if (Mzhuan != "")
            {
                string insertStr = "UPDATE StudentTable SET 籍贯 = '" + Mzhuan + "' WHERE  学号='" + Mno + "'";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            if (Mcelllphone != "")
            {
                string insertStr = "UPDATE StudentTable SET 手机号码 = '" + Mcelllphone + "' WHERE   学号='" + Mno + "'";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            if (Mming != "")
            {
                string insertStr = "UPDATE StudentTable SET 民族 = '" + Mming + "' WHERE   学号='" + Mno + "'";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            if (Mdept != "")
            {
                string insertStr = "UPDATE StudentTable SET 年级 = '" + Mdept + "' WHERE   学号='" + Mno + "'";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
        }
        catch
        {
            MessageBox.Show("输入数据违反要求!");
        }
        finally
        {
            con.Dispose();
        }

        this.studentTableTableAdapter.Fill(this.sTUDENTDataSet2.StudentTable);
    }

    private void button4_Click_1(object sender, EventArgs e)
    {

            String Mno = textBox1.Text.Trim();
            String Mname = textBox2.Text.Trim();
            String Msex = textBox3.Text.Trim();
            String Mbirth = textBox4.Text.Trim();
            String Mzhuan = textBox5.Text.Trim();
            String Mdept = textBox6.Text.Trim();
            String Mcelllphone = textBox7.Text.Trim();
            String Mming = textBox8.Text.Trim();

            String conn = "Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123";
            SqlConnection sqlConnection = new SqlConnection(conn);
        try
        {

            if (Mno != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from StudentTable where 学号='" + Mno + "'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Mname != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from StudentTable where 姓名 Like'" + Mname + "%'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Msex != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from StudentTable where 性别='" + Msex + "'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Mbirth != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from StudentTable where 出生日期 Like'" + Mbirth + "%'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Mzhuan != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from StudentTable where 专业 Like'" + Mzhuan + "%'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Mcelllphone != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from StudentTable where 手机号码='" + Mcelllphone + "'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Mming != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from StudentTable where 民族 Like'" + Mming + "%'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Mdept != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from StudentTable where 专业 Like'" + Mdept + "%'";
                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("查询失败!!!");
        }
        finally
        {
            sqlConnection.Close();
        }
    }

    private void textBox7_TextChanged(object sender, EventArgs e)
    {

    }
}
}

在这里插入图片描述

课程信息界面代码:

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 managercourse : Form
{
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123");
    public managercourse()
    {
        InitializeComponent();
    }

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

    }

    private void button1_Click(object sender, EventArgs e)
    {
        String Cno = textBox1.Text.Trim();//课程号
        String Cname = textBox2.Text.Trim();//课程名
        String Credit = textBox3.Text.Trim();//学分
        try
        {
            con.Open();
            string insertStr = "INSERT INTO Course(课程号,课程名,学分) " +
                "VALUES('" + Cno + "','" + Cname + "','" + Credit + "')";
            SqlCommand cmd = new SqlCommand(insertStr, con);
            cmd.ExecuteNonQuery();
        }
        catch
        {
            MessageBox.Show("输入数据违反要求");
        }
        finally
        {
            con.Dispose();
        }
        this.courseTableAdapter.Fill(this.sTUDENTDataSet2.Course);

    }

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string delete_by_id = "delete from Course where 课程号=" + select_id;
            SqlCommand cmd = new SqlCommand(delete_by_id, con);
            cmd.ExecuteNonQuery();
        }
        catch
        {
            MessageBox.Show("请正确选择行!");
        }
        finally
        {
            con.Dispose();
        }
        this.courseTableAdapter.Fill(this.sTUDENTDataSet2.Course);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        String Cno = textBox1.Text.Trim();//课程号
        String Cname = textBox2.Text.Trim();//课程名
        String Credit = textBox3.Text.Trim();//学分
        try
        {
            con.Open();
            if (Cname != "")
            {
                string insertStr = "UPDATE Course SET 课程名 = '" + Cname + "' WHERE   课程号='" + Cno + "'";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            if (Credit != "")
            {
                string insertStr = "UPDATE Course SET 学分 = '" + Credit + "' WHERE   课程号='" + Cno + "'";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
        }
        catch
        {
            MessageBox.Show("修改出错!");
        }
        finally
        {
            con.Dispose();
        }
        this.courseTableAdapter.Fill(this.sTUDENTDataSet2.Course);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        String Cno = textBox1.Text.Trim();//课程号
        String Cname = textBox2.Text.Trim();//课程名
        String Credit = textBox3.Text.Trim();//学分
        String conn = "Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123";
        SqlConnection sqlConnection = new SqlConnection(conn);
        try
        {
            if (Cno != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from Course where 课程号='" + Cno + "'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Cname != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from Course where 课程名 Like'" + Cname + "%'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Credit != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from Course where 学分='" + Credit + "'";
                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("查询语句有误!");
        }
        finally
        {
            sqlConnection.Close();
        }
    }

    private void button5_Click(object sender, EventArgs e)
    {
        Manager manager = new Manager();
        manager.Show();
        this.Hide();
    }
}
}

在这里插入图片描述

学生成绩信息界面代码:

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 managercs : Form
{
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123");
    public managercs()
    {
        InitializeComponent();
    }

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

    }

    private void button5_Click(object sender, EventArgs e)
    {
        Manager manager = new Manager();
        manager.Show();
        this.Hide();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        String Sno = textBox1.Text.Trim();
        String Sname = textBox2.Text.Trim();
        String Cno = textBox3.Text.Trim();
        String Cname = textBox4.Text.Trim();
        String Grade = textBox5.Text.Trim();
        String Credit = textBox6.Text.Trim();

        try
        {
            con.Open();
            if (Grade != "" && Credit != "")
            {
                string insertStr = "INSERT INTO SC(学号,姓名,课程号,课程名,成绩,学分) " +
                "VALUES('" + Sno + "','" + Sname + "','" + Cno + "','" + Cname + "'," + Grade + "," + Credit + ")";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
        }
        catch
        {
            MessageBox.Show("输入数据违反要求");
        }
        finally
        {
            con.Dispose();
        }
        this.sCTableAdapter.Fill(this.sTUDENTDataSet2.SC);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            string select_id1 = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string select_id2 = dataGridView1.SelectedRows[0].Cells[2].Value.ToString().Trim();
            string delete_by_id = "delete from SC where 学号='" + select_id1 + "' AND 课程号='" + select_id2 + "'";
            SqlCommand cmd = new SqlCommand(delete_by_id, con);
            cmd.ExecuteNonQuery();
        }
        catch
        {
            MessageBox.Show("请正确选择行!");
        }
        finally
        {
            con.Dispose();
        }
        this.sCTableAdapter.Fill(this.sTUDENTDataSet2.SC);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        String Sno = textBox1.Text.Trim();
        String Cno = textBox3.Text.Trim();
        String Grade = textBox5.Text.Trim();
        try
        {
            con.Open();
            string insertStr = "UPDATE SC SET 成绩 = " + Grade + " WHERE   学号='" + Sno + "' AND 课程号='" + Cno + "'";
            SqlCommand cmd = new SqlCommand(insertStr, con);
            cmd.ExecuteNonQuery();
        }
        catch
        {
            MessageBox.Show("输入数据违反要求");
        }
        finally
        {
            con.Dispose();
        }
        this.sCTableAdapter.Fill(this.sTUDENTDataSet2.SC);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        String Sno = textBox1.Text.Trim();
        String Sname = textBox2.Text.Trim();
        String Cno = textBox3.Text.Trim();
        String Cname = textBox4.Text.Trim();
        String Grade = textBox5.Text.Trim();
        String Credit = textBox6.Text.Trim();
        String conn = "Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123";
        SqlConnection sqlConnection = new SqlConnection(conn);
        try
        {
            if (Sno != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 学号='" + Sno + "'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Sname != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 姓名 Like'" + Sname + "%'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Cno != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 课程号='" + Cno + "'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Cname != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 课程名 Like'" + Cname + "%'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Grade != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 成绩 =" + Grade;
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Credit != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 学分=" + Credit;
                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("查询语句有误!");
        }
        finally
        {
            sqlConnection.Close();
        }
    }
}
}

在这里插入图片描述
普通用户界面代码:

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)
    {
        

    }

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

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

    private void button4_Click(object sender, EventArgs e)
    {
        Form4 form4 = new Form4();
        form4.Show();
        this.Hide();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        STUCOURSE sTUCOURSE = new STUCOURSE();
        sTUCOURSE.Show();
        this.Hide();
    }
}
}

在这里插入图片描述

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 Form4 : Form
{
    public Form4()
    {
        InitializeComponent();
    }
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123");
    private void Form4_Load(object sender, EventArgs e)
    {
        // TODO: 这行代码将数据加载到表“sTUDENTDataSet2.SC”中。您可以根据需要移动或删除它。
        this.sCTableAdapter.Fill(this.sTUDENTDataSet2.SC);

    }

    private void button4_Click(object sender, EventArgs e)
    {
        String Sno = textBox1.Text.Trim();
        String Sname = textBox2.Text.Trim();
        String Cno = textBox3.Text.Trim();
        String Cname = textBox4.Text.Trim();
        String Grade = textBox5.Text.Trim();
        String Credit = textBox6.Text.Trim();
        String conn = "Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123";
        SqlConnection sqlConnection = new SqlConnection(conn);
        try
        {
            if (Sno != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 学号='" + Sno + "'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Sname != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 姓名 Like'" + Sname + "%'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Cno != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 课程号='" + Cno + "'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Cname != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 课程名 Like'" + Cname + "%'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Grade != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 成绩 =" + Grade;
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            if (Credit != "")
            {
                sqlConnection.Open();
                String select_by_id = "select * from SC where 学分=" + Credit;
                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("查询语句有误!");
        }
        finally
        {
            sqlConnection.Close();
        }
    }

    private void button5_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
        this.Hide();
    }
}
}

在这里插入图片描述

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 STUCOURSE : Form
{
    public STUCOURSE()
    {
        InitializeComponent();
    }
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STUDENT;User ID=sa;Password=woaini123");
    private void STUCOURSE_Load(object sender, EventArgs e)
    {
        // TODO: 这行代码将数据加载到表“sTUDENTDataSet2.Course”中。您可以根据需要移动或删除它。
        this.courseTableAdapter.Fill(this.sTUDENTDataSet2.Course);

    }

    private void button5_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
        this.Hide();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        String Cno = textBox1.Text.Trim();//课程号
        String Cname = textBox2.Text.Trim();//课程名
        String Credit = textBox3.Text.Trim();//学分
        try
        {
            con.Open();
            string insertStr = "INSERT INTO Course(课程号,课程名,学分) " +
                "VALUES('" + Cno + "','" + Cname + "','" + Credit + "')";
            SqlCommand cmd = new SqlCommand(insertStr, con);
            cmd.ExecuteNonQuery();
        }
        catch
        {
            MessageBox.Show("输入数据违反要求");
        }
        finally
        {
            con.Dispose();
        }
        this.courseTableAdapter.Fill(this.sTUDENTDataSet2.Course);
    }
}
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值