數據庫大作業,代碼展示

在這裏插入圖片描述
登錄界面代碼:

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);
    }
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章