C#数据库大作业

用C#写的,比较一般,该篇博客记录部分代码
部分同学找我要代码,github传一下: C#数据库大作业代码

登录界面

在这里插入图片描述
代码

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

    public partial class Sign1 : Form
    {
       bool showPW = false;
       public static string user = "";
       public static string id = "201215121";
       public static string yzm = "";
        public Sign1()
        {
            InitializeComponent();
            textBox2.PasswordChar = '*';//输入密码以 * 展示
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Random1();

        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text.Trim() == "学生") user = "学生";
            else if (comboBox1.Text.Trim() == "管理员") user = "管理员";
        }
        private void button1_Click(object sender, EventArgs e)
        {

            if (user == "")
            {
                MessageBox.Show("请选择身份");
                return;
            }
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入账号/学号");
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("请输入账号/学号");
                return;
            }
            if (textBox3.Text == "")
            {
                MessageBox.Show("请输入验证码");
                return;
            }
            if (textBox3.Text != yzm)
            {
                MessageBox.Show("验证码输入错误");
                return;
            }
            string table = "";
            if (user == "学生") table = "Student_Account";
            else if (user == "管理员") table = "Manager_Account";
            id = textBox1.Text.Trim();
            string password = EncryptWithMD5(textBox2.Text.Trim());
            String select1 = "select * from "+table+" where account='" + id + "'";
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            con.Open();
            SqlCommand sqlCommand = new SqlCommand(select1, con);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();//读取数据
            if (sqlDataReader.HasRows)//判断账号是否存在
            {
                sqlDataReader.Close();
                String select2= "select * from " + table + " where account='" + id + "' AND password='"+password+"'";
                SqlCommand sqlCommand2 = new SqlCommand(select2, con);
                SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
                if (sqlDataReader2.HasRows)
                {
                    MessageBox.Show("登录成功");
                    if (user == "学生")
                    {
                        Student s = new Student();
                        s.Show();
                    }
                    else
                    {
                        Manager m = new Manager();
                        m.Show();
                    }
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("密码输入错误");
                    return;
                }
            }
            else
            {
                MessageBox.Show("不存在该账号");
                return ;
            }

            Sign1.id = textBox1.Text.Trim();
        }
        private void Random1()
        {
            Bitmap map = new Bitmap(100, 35);
            Graphics c = Graphics.FromImage(map);
            
            Random myrandom = new Random();
            string str = "";
            for (int i = 0; i < 5; i++)
            {
                int x = myrandom.Next(0, 10);
                str += x;
            }
            yzm = str;
            string[] myfont = { "宋体", "楷体", "仿宋", "隶书", "微软雅黑" };
            Color[] mycolor = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Brown };

            for (int i = 0; i < 5; i++)
            {
                Point p = new Point(i * 20, 0);
                c.DrawString(str[i].ToString(), new Font(myfont[i], 16, FontStyle.Bold), new SolidBrush(mycolor[i]), p);

            }


            for (int i = 0; i < 500; i++)
            {
                Point p = new Point(myrandom.Next(0, map.Width), myrandom.Next(0, map.Height));
                map.SetPixel(p.X, p.Y, Color.Black);
            }
            pictureBox1.Image=map;
        }
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            Random1();
        }


        private void button2_Click(object sender, EventArgs e)//注册
        {
            if (user == "管理员")
            {
                Register f3 = new Register();
                f3.Show();
            }
            else
            {
                Register1 ff = new Register1();
                ff.Show();
            }   
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            showPW = !showPW;
            if (showPW)
            {
                textBox2.PasswordChar = '\0';//输入密码以实际输入结果展示
               
            }
            else
            {
                textBox2.PasswordChar = '*';//输入密码以 * 展示
               
            }


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

        }
        private void label4_Click(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 linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (user == "") MessageBox.Show("请选择身份");
            else
            {
                Forget fg = new Forget();
                fg.Show();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Random1();
        }
    }
}

学生或者管理员注册界面

在这里插入图片描述
代码

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

namespace login
{
    public partial class Register1 : Form
    {
        public Register1()
        {
            InitializeComponent();
            textBox2.PasswordChar = '*';//输入密码以 * 展示
            textBox3.PasswordChar = '*';
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            string table = "Student_Account";
            string account = textBox1.Text.Trim();
            string password = textBox2.Text.Trim();
            string password1 = textBox3.Text.Trim();
            string question = textBox4.Text.Trim();
            string answer = textBox5.Text.Trim();
            if (account == "")
            {
                MessageBox.Show("学号不可为空");
                return;
            }
            if (password == "")
            {
                MessageBox.Show("新密码不可为空");
                return;
            }
            if (question == "")
            {
                MessageBox.Show("密钥问题不可为空");
                return;
            }
            if (answer == "")
            {
                MessageBox.Show("密钥答案不可为空");
            }
            if (picturePath == "")
            {
                MessageBox.Show("请上传图片!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (password1 != password) MessageBox.Show("两次密码输入不一致");
            else
            {
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象 
                con.Open();
                String select = "select * from Student where Sno ='" + textBox1.Text + "'";
                SqlCommand sqlCommand = new SqlCommand(select, con);
                SqlDataReader m = sqlCommand.ExecuteReader();
                if (!m.HasRows)
                {
                    MessageBox.Show("不存在该学号!");
                    m.Close();
                    con.Close();
                    return;
                }
                m.Close();
                String id = "select * from " + table + " where Account ='" + textBox1.Text + "'";
                SqlCommand sqlCommand1 = new SqlCommand(id, con);
                SqlDataReader m1 = sqlCommand1.ExecuteReader();
                if (m1.HasRows)
                {
                    MessageBox.Show("学生账户已存在!");
                    m1.Close();
                    con.Close();
                    return;
                }
                m1.Close();
                string sql = "insert into Student_Account (account,password,question,answer,photo) " +
                                                        "values (@account, @password,@question,@answer,@photo)";
                SqlCommand command = new SqlCommand(sql, con);
                SqlParameter sqlParameter = new SqlParameter("@account", account);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@password", EncryptWithMD5(password));
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@question",question);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@answer", answer);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@photo", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
                command.Parameters.Add(sqlParameter);
                command.ExecuteNonQuery();

                MessageBox.Show("注册成功");
                this.Close();
            }
        }
        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();
        }

        public Byte[] mybyte = new byte[0];
        string picturePath = "";
        private void button2_Click(object sender, EventArgs e)//头像
        {
            //打开浏览图片对话框
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.ShowDialog();
            picturePath = openFileDialog.FileName;//获取图片路径
                                                  //文件的名称,每次必须更换图片的名称,这里很为不便
                                                  //创建FileStream对象
            if (picturePath == "")
            {
                return;
            }
            FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
            //声明Byte数组
            mybyte = new byte[fs.Length];
            //读取数据
            fs.Read(mybyte, 0, mybyte.Length);
            pictureBox1.Image = Image.FromStream(fs);
            fs.Close();

        }
       
    }
}

学生或者管理员忘记密码

在这里插入图片描述
代码

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 login
{
    public partial class Forget : Form
    {
        String table;
        int flag=0;
        public Forget()
        {
            InitializeComponent();
            if (Sign1.user == "学生") label1.Text = "学生重置密码";
            else if (Sign1.user == "管理员") label1.Text = "管理员重置密码";
            label7.Text = "";
        }

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

       

        private void label2_Click(object sender, EventArgs e)
        {

        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            String account = textBox1.Text.Trim();
            String password = textBox4.Text.Trim();
            String password1 = textBox5.Text.Trim();
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            con.Open();           
            if (flag == 1)//存在账号
            {
                if (password == "")
                {
                    MessageBox.Show("请输入新密码");
                    return;
                }
                if (password == password1)
                {
                    string ans = "select * from " + table + " where account ='" + textBox1.Text + "'";
                    SqlDataAdapter sda = new SqlDataAdapter(ans, con);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    String s = dt.Rows[0][3].ToString();
                    if (string.Compare(textBox3.Text.Trim(), s.Trim()) != 0)
                    {
                        MessageBox.Show("答案错误!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    string updateStr = "UPDATE "+table+" SET password = '" + EncryptWithMD5(password) + 
                    "' WHERE account = '" + account + "'";
                     SqlCommand cmd = new SqlCommand(updateStr, con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("修改密码成功");
                    this.Close();

                }
                else
                {
                    MessageBox.Show("两次密码输入不一致");
                    return;
                }
               
            }
            else
            {
                MessageBox.Show("未获取密匙问题");
            }
            
           
            

        }
        //密码使用MD5加密
        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)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            if (textBox1.Text == "")
            {
                MessageBox.Show("账号不能为空", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                textBox1.Focus();
                return;
            }
            con.Open();
            if (Sign1.user == "学生") table = "Student_Account";
            else table = "Manager_Account";
            String id = "select * from "+table+" where account ='" + textBox1.Text + "'";
            SqlCommand comm = new SqlCommand(id, con);
            SqlDataReader m = comm.ExecuteReader();
            if (!m.HasRows)
            {
                MessageBox.Show("不存在该账号!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m.Close();
                con.Close();
                return;
            }
            m.Close();
            SqlDataAdapter sda = new SqlDataAdapter(id, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            label7.Text = dt.Rows[0][2].ToString();
            con.Close();
           
            flag = 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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace login
{
    public partial class Student : Form
    {
        
        public Student()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            InitializeComponent();
            label6.Text = Sign1.id;
            String select_by_id = "select * from Student_Account where Account='" + Sign1.id + "'";
            SqlDataAdapter sda = new SqlDataAdapter(select_by_id, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            Byte[] picture = new Byte[0];
            picture = (Byte[])(dt.Rows[0][4]);
            MemoryStream stream = new MemoryStream(picture);
            pictureBox1.Image = Image.FromStream(stream);
           
        }
        public static string id = Sign1.id;
        private void Student_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            try
            {
                con.Open();
                String select_by_id = "Select * from Student where Sno = '" + label6.Text + "'";
                SqlDataAdapter sda = new SqlDataAdapter(select_by_id, con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                string name = dt.Rows[0][1].ToString();
                string sex = dt.Rows[0][2].ToString();
                string age = dt.Rows[0][3].ToString();
                string dept=dt.Rows[0][4].ToString();
                label7.Text = name.Trim();
                label8.Text = sex.Trim();
                label9.Text = age.Trim();
                label10.Text = dept.Trim();
            }
            catch
            {
                MessageBox.Show("ERROR!");
            }
            finally
            {
                con.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)//课表
        {
            MyCourse M1 = new MyCourse();
            M1.Show();
        }

        private void button4_Click(object sender, EventArgs e)//成绩
        {
            MyGrade g = new MyGrade();
            g.Show();
        }

        private void button5_Click(object sender, EventArgs e)//选课退课
        {
            MySC c = new MySC();
            c.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Sign1 f1 = new Sign1();
            f1.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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace login
{
    public partial class MySC : Form
    {
        public MySC()
        {
            InitializeComponent();
        }
        String id = Sign1.id;
        private void MySC_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            // TODO: 这行代码将数据加载到表“stuDataSet.SC”中。您可以根据需要移动或删除它。
            this.sCTableAdapter.Fill(this.stuDataSet.SC);
            con.Open();
            String select_by_id = "select * from Course";
            SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = sqlDataReader;
            dataGridView1.DataSource = bindingSource;
            sqlDataReader.Close();
            String select_by_id2 = "Select Course.Cno,Cname,Cpno,Ccredit from Course,SC where SC.Cno = Course.Cno AND SC.Sno = '" + id + "'";
            SqlCommand sqlCommand2 = new SqlCommand(select_by_id2, con);
            SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
            BindingSource bindingSource2 = new BindingSource();
            bindingSource2.DataSource = sqlDataReader2;
            dataGridView2.DataSource = bindingSource2;
            con.Close();
            
            label5.Text = id;
        }

        private void button1_Click(object sender, EventArgs e)//退课
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            try
            {
                con.Open();
                string Cno = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
                string delete_by_id = "delete from SC where Sno=" + id + "and Cno='" + Cno + "'";//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_id, con);
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("请正确选择行!");
            }
            finally
            {
                
                con.Close();
            }
            this.sCTableAdapter.Fill(this.stuDataSet.SC);
            con.Open();
            String select_by_id2 = "Select Course.Cno,Cname,Cpno,Ccredit from Course,SC where SC.Cno = Course.Cno AND SC.Sno = '" + id + "'";
            SqlCommand sqlCommand2 = new SqlCommand(select_by_id2, con);
            SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
            BindingSource bindingSource2 = new BindingSource();
            bindingSource2.DataSource = sqlDataReader2;
            dataGridView2.DataSource = bindingSource2;
            con.Close();
        }

        private void button3_Click(object sender, EventArgs e)//查询
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            String Cno = textBox1.Text.Trim();
            try
            {
                con.Open();
                String select_by_id = "select * from Course where Cno='" + Cno + "'";
                SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            catch
            {
                MessageBox.Show("不存在该课程");
            }
            finally
            {
                con.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)//选课
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            String Cno = textBox1.Text.Trim();
            con.Open();
            string insertStr = "INSERT INTO  SC (Sno,Cno,Grade)" +
                    "VALUES ('" + id + "','" + Cno + "',"+ "NULL)";
            try { 
            SqlCommand cmd = new SqlCommand(insertStr,con);
            cmd.ExecuteNonQuery();        
            }
            catch
            {
                MessageBox.Show("查询语句错误"+insertStr);
            }
            finally
            {
                con.Dispose();
            }
            this.sCTableAdapter.Fill(this.stuDataSet.SC);
            this.sCTableAdapter.Fill(this.stuDataSet.SC);
            String conn2 = "Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456";
            SqlConnection con2 = new SqlConnection(conn2);  //实例化连接对象
            con2.Open();
            String select_by_id2 = "Select Course.Cno,Cname,Cpno,Ccredit from Course,SC where SC.Cno = Course.Cno AND SC.Sno = '" + id + "'";
            SqlCommand sqlCommand2 = new SqlCommand(select_by_id2, con2);
            SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
            BindingSource bindingSource2 = new BindingSource();
            bindingSource2.DataSource = sqlDataReader2;
            dataGridView2.DataSource = bindingSource2;
            con2.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)//刷新
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            con.Open();
            String select_by_id = "select * from Course";
            SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = sqlDataReader;
            dataGridView1.DataSource = bindingSource;
            sqlDataReader.Close();
            String select_by_id2 = "Select Course.Cno,Cname,Cpno,Ccredit from Course,SC where SC.Cno = Course.Cno AND SC.Sno = '" + id + "'";
            SqlCommand sqlCommand2 = new SqlCommand(select_by_id2, con);
            SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
            BindingSource bindingSource2 = new BindingSource();
            bindingSource2.DataSource = sqlDataReader2;
            dataGridView2.DataSource = bindingSource2;
        }
    }
}

管理员界面

在这里插入图片描述

学生信息管理

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码

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

namespace login
{
    public partial class Manager : Form
    {
        public static string table = "";
        public Manager()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StudentTable f1 = new StudentTable();
            f1.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

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

        private void button4_Click(object sender, EventArgs e)
        {
            SCTable f4 = new SCTable();
            f4.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
            Sign1 f5 = new Sign1();
            f5.Show();
        }

        private void button6_Click(object sender, EventArgs e)//账户信息管理
        {
            Account ac = new Account();
            ac.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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace login
{
    public partial class Account : Form
    {
 
       
        public Account()
        {
            InitializeComponent();
            refresh();
            
        }
        private void refresh()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            con.Open();
            String select_by_id = "select * from Manager_Account";
            SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = sqlDataReader;
            dataGridView1.DataSource = bindingSource;
            sqlDataReader.Close();
            String select_by_id2 = "Select account,password,question,answer from Student_Account";
            SqlCommand sqlCommand2 = new SqlCommand(select_by_id2, con);
            SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
            BindingSource bindingSource2 = new BindingSource();
            bindingSource2.DataSource = sqlDataReader2;
            dataGridView2.DataSource = bindingSource2;
            con.Close();
        }
        String table = "Manager_Account";
        private void Account_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“stuDataSet3.Student_Account”中。您可以根据需要移动或删除它。
            this.student_AccountTableAdapter1.Fill(this.stuDataSet3.Student_Account);
            // TODO: 这行代码将数据加载到表“stuDataSet3.Manager_Account”中。您可以根据需要移动或删除它。
            this.manager_AccountTableAdapter2.Fill(this.stuDataSet3.Manager_Account); 
        }

        private void button1_Click(object sender, EventArgs e)//删除
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu;User ID=sa;Password=123456");  //实例化连接对象
            try
            {

                con.Open();
                string select_id = "";
                if(table=="Manager_Account") select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
                else select_id = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
                string delete_by_id = "delete from "+table+" where Account=" + select_id;//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_id, con);
                
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("请正确选择行!"+table);
            }
            finally
            {
                con.Dispose();
            }
            this.manager_AccountTableAdapter.Fill(this.stuDataSet1.Manager_Account);
            refresh();
        }

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

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

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }
     

        private void tabControl1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabPage1) table = "Manager_Account";
            else if (tabControl1.SelectedTab == tabPage2) table = "Student_Account";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (table == "Student_Account")
            {
                Register1 r = new Register1();
                r.Show();
            }
            else
            {
                Register r1 = new Register();
                r1.Show();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            refresh();
        }
    }
}

数据库建表

有一部分人问我数据库怎么建的表。
我用的是SQL Server,建的表大概就下面这几个,不过时间比较久了,我也记不太清了,也可能有漏掉的
Manager_Account
在这里插入图片描述

Student_Account
在这里插入图片描述
Course
在这里插入图片描述
SC
在这里插入图片描述
Student
在这里插入图片描述

  • 24
    点赞
  • 156
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值