基于sqlserver数据库的学生成绩管理系统

目录

学生数据编辑界面: 

学生数据查询界面: 

完整项目源码:https://download.csdn.net/download/qq_40323256/10777385

文档介绍:https://wenku.baidu.com/view/96a3284c53d380eb6294dd88d0d233d4b04e3f76


 

 

     “用户登录”为“groupBox控件”

 双击“确定”按钮:

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "sa")
            {
                if (textBox2.Text == "123") Program.show();
                else MessageBox.Show("密码错误!");
            }
            else MessageBox.Show("用户名错误!");
        }

学生数据编辑界面: 

加载窗体以及初始化时的代码:

public Form1()
        {
            InitializeComponent();
        }
        public static string mystr = "server=DESKTOP-NA2OPPJ\\SQLEXPRESS;database=学生选课4;uid=sa;pwd=123";
        public static string sqlstr = "select*from student";
        static SqlConnection myconn = new SqlConnection(mystr);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, myconn);
        DataSet myds = new DataSet();
        DataView mydv = new DataView();
        int x;
        int y;
        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            groupBox1.Visible = false;
            groupBox2.Visible = false;
            label6.Visible = true;
            button4.Visible = false;
        }

Programs.cs: 

using System;
using System.Windows.Forms;
namespace XueShengXuanKeXiTong
{
    static class Program
    {
        static Form1 form1;
        static Form2 form2;
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            form2 = new Form2();
            form2.ShowDialog();
        }
        public static void show()
        {
            form2.Dispose();
            form1 = new Form1();
            form1.ShowDialog();
        }
        public static void close()
        {
            form1.Dispose();
            form2 = new Form2();
            form2.ShowDialog();
        }
    }
}

双击“新增”按钮:

        private void XZbutton_Click(object sender, EventArgs e)
        {
            myconn.Open();
            string insertCmd;
            try
            {
                switch (y)
                {
                    case 0:
                        {
                            insertCmd = "Insert Into student(学号,姓名,性别,年龄,班级) Values(@s1,@s2,@s3,@s4,@s5)";
                            SqlCommand cmd;
                            cmd = new SqlCommand(insertCmd, myconn);
                            cmd.Parameters.Add(new SqlParameter("@s1", SqlDbType.Char));
                            cmd.Parameters.Add(new SqlParameter("@s2", SqlDbType.Char));
                            cmd.Parameters.Add(new SqlParameter("@s3", SqlDbType.Char));
                            cmd.Parameters.Add(new SqlParameter("@s4", SqlDbType.Int));
                            cmd.Parameters.Add(new SqlParameter("@s5", SqlDbType.Char));
                            cmd.Parameters["@s1"].Value = textBox1.Text;
                            cmd.Parameters["@s2"].Value = textBox2.Text;
                            cmd.Parameters["@s3"].Value = textBox4.Text;
                            cmd.Parameters["@s4"].Value = int.Parse(textBox3.Text);
                            cmd.Parameters["@s5"].Value = textBox5.Text;
                            cmd.ExecuteNonQuery();
                            myds.Clear();
                            sqlstr = "select*from student";
                            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, myconn);
                            myda.Fill(myds, "student");
                            mydv = myds.Tables["student"].DefaultView;
                            dataGridView1.DataSource = mydv;
                        }
                        break;
                }
            }
            catch
            {
                MessageBox.Show("请输入正确信息!", "错误");
            }
            myconn.Close();
        }

双击“删除”按钮(注意:这是根据学号删除,即只需输入学号即可删除记录):

private void SCbutton_Click(object sender, EventArgs e)
        {
            myconn.Open();
            string delCmd;
            try
            {
                switch (y)
                {
                    case 0:
                        {
                            delCmd = "Delete From student Where 学号=@s1";
                            SqlCommand cmd;
                            cmd = new SqlCommand(delCmd, myconn);
                            cmd.Parameters.Add(new SqlParameter("s1", SqlDbType.Char));
                            cmd.Parameters["s1"].Value = textBox1.Text;
                            cmd.ExecuteNonQuery();
                            myds.Clear();
                            sqlstr = "select*from student";
                            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, myconn);
                            myda.Fill(myds, "student");
                            mydv = myds.Tables["student"].DefaultView;
                            dataGridView1.DataSource = mydv;
                            myconn.Close();
                        }
                        break;
                }
            }
            catch
            {
                MessageBox.Show("请输入正确信息!", "错误");
            }
        }

 双击“排序”下的“确定”按钮:

        private void button3_Click(object sender, EventArgs e)
        {
            string order = "";
            if (comboBox3.Text != "")
            {
                if (radioButton1.Checked) order = comboBox3.Text + " ASC";
                else order = comboBox3.Text + " DESC";
            }
            mydv.Sort = order;
        }

“更新”按钮:

        private void button4_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder mycom = new SqlCommandBuilder(myda);
            switch (x)
            {
                case 0: myda.Update(myds, "student"); break;
            }
        }

学生数据查询界面: 

 查询时的“确定”按钮:

        private void button1_Click(object sender, EventArgs e)
        {
            string condstr = "";
            switch (x)
            {
                case 0:
                    {
                        if (textBox1.Text != "") condstr = "学号 = " + textBox1.Text + "";
                        if (textBox2.Text != "")
                        {
                            if (condstr != "") condstr = condstr + "and 姓名 like'" + textBox2.Text + "%'";
                            else condstr = "姓名 like'" + textBox2.Text + "%'";
                        }
                        if (comboBox1.Text != "")
                        {
                            if (condstr != "") condstr = condstr + "and 性别 like'" + comboBox1.Text + "'";
                            else condstr = "性别 like'" + comboBox1.Text + "'";
                        }
                        if (textBox3.Text != "")
                        {
                            if (condstr != "") condstr = condstr + "and 年龄 " + textBox3.Text + "";
                            else condstr = "年龄 =" + textBox3.Text + "";
                        }
                        if (comboBox2.Text != "")
                        {
                            if (condstr != "") condstr =condstr+ "and 班级 like '" + comboBox2.Text + "'";
                            else condstr = "班级 like'" + comboBox2.Text + "'";
                        }
                    }
                    break;
            }
            mydv.RowFilter = condstr;
        }

查询时的“重置”按钮:

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            comboBox1.Text = "";
            comboBox2.Text = "";
            comboBox4.Text = "";
        }

 运行结果:

完整项目源码:https://download.csdn.net/download/qq_40323256/10777385

文档介绍:https://wenku.baidu.com/view/96a3284c53d380eb6294dd88d0d233d4b04e3f76

附截图:

猜你喜欢

转载自blog.csdn.net/qq_40323256/article/details/83934820