基于C#实现SQLServer班级管理系统

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39378077/article/details/78868956

一.    原理

1.实现数据库的连接

2.时间窗体与数据库记录的传输

主要利用DateBindings属性。

DataBindings属性是很多控件都有的属性,作用有2方面。一方面是用于与数据库的数据进行绑定,进行数据显示。另一方面用于与控件或类的对象进行数据绑定。

Label、TextBox等都包含DataBindings属性,其类型为ControlBindingsCollection,是Binding类的集合。Binding类代表某对象属性值和某控件属性值之间的简单绑定。如可以将TextBox的Text属性值绑定到Label的Text属性值,这样,当TextBox中的文本被修改的时候,Label的文本也会及时进行修改,如下面的代码所示:

Label1.DataBindings.Add("Text",TextBox1,"Text");

当使用Binding的构造函数创建实例时,必须指定三项内容:

·        要绑定到的控件属性的名称

·        数据源

·        数据源中解析为列表或属性的导航路径

其中,数据源可以为:

·        实现 IBindingList 或 ITypedList 的任何类。包括:DataSet、DataTable、DataView 或 DataViewManager。 

·        实现 IList 的任意索引集合类。(必须在创建 Binding 之前创建和填充该集合,并且列表中的所有对象必须为同一类型,否则将引发异常) 

·        强类型对象的强类型 IList。

3. dataGridView

4.利用BindingManagerBase可以对数据进行元组的上翻与下翻,并且可以计数

 

二.    重点功能介绍

1.    登录功能

2.    利用dataGridView进行查询,同时利用代码把导入的表格进行保存,实行增加和删除修改功能。

3.    利用DateBindings属性将查询值直接传入控件,代码更为复杂,但是交互性更好。同时事项修改功能。

4.    利用BindingManagerBase可以对数据库的元组进行上翻和下翻功能,同时能够进行计数来显示元组数量和当前元组的序号。

bmb.PositionChanged+= newEventHandler(bmb_PositionChanged);

label6.Text =(bmb.Position + 1).ToString()+"/"+bmb.Count;

三.    SqlServer中实现功能

建立student数据库,包含表格studentinfo(管理学生信息),teacherinfo(教师信息管理),majorinfo(学院信息管理),classinfo(班级信息管理),courseinfo(课程信息管理),resultinfo(成绩管理),userinfo(登录信息管理)。

所含信息与表间联系如下:

其中userinfo.user_Type包含1,2,3三种类型,1对应学生,2对应,教师,3对应管理人员。

四.    C#中实现功能

利用C#与sqlServer实现学籍教师课程管理系统数据库

1.窗体FORM1

设置背景图片,加载menuStrip,添加按钮包含功能:学籍管理,班级管理,教师管理,课程管理,成绩管理,系统管理。

利用以下代码进行窗体之间的连接:

private void 班级查看ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            班级简介 fbj = new 班级简介();
            fbj.Show();
        }

2.登录窗体


密码用textbox2passwordchar设置为*

接下来与数据库连接,并核对信息,当学号密码正确,并且用户类型为1即为学生时弹出登录成功界面,并且打开下一个学籍管理窗体,当任意一个不满足,则弹出登录失败界面。

public static string name1;
//建立了一个公用变量,用于传递接下来用到的学号
private void button1_Click(object sender, EventArgs e)
        {
            string name,pass,type;
            
            
            if (textBox1.Text == "")
                MessageBox.Show("用户名不能为空!", "提示");
            else if (textBox2.Text == "")
                MessageBox.Show("密码不能为空!", "提示");
            try //try...catch...异常处理语句
            {
                
                bool flag = false;
                name = textBox1.Text;
                pass = textBox2.Text;
                type = "1";
                name1 = name;//获取用户名,密码
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "Data Source=localhost;Initial Catalog=student;Integrated Security=SSPI;";
                con.Open();
                string sqlstring = "select user_ID,password from userinfo where user_ID='" + name + "'and password='" + pass + "'and user_Type='"+type +"'";
                //执行con对象的函数,返回一个SqlCommand类型的对象
                SqlCommand command = new SqlCommand(sqlstring, con);
                //用cmd的函数执行语句,返回SqlDataReader对象thisReader,thisReader就是返回的结果集(也就是数据库中查询到的表数据)
                SqlDataReader thisReader = command.ExecuteReader();
                //判断用户名及密码是否正确,对flag进行赋值
                while (thisReader.Read())
                {
                    if ((thisReader.GetValue(0).ToString().Trim()) == (name.ToString().Trim()))
                    {
                        if (thisReader.GetValue(1).ToString().Trim() == pass.ToString().Trim())
                        {
                            flag = true;
                        }
                    }
                }
                if (flag)
                {
                    MessageBox.Show("登陆成功!");
                    Fxueji F = new Fxueji(); //显示主页面
                    F.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("请检查你的用户名和密码!");
                    textBox1.Focus();
                }
            }
           catch (Exception ex2)
               {
                    MessageBox.Show("连接远程SQL数据库发生错误:" + ex2.ToString(), "错误!");
               }
            
          }

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

3.学籍管理窗体

学籍管理,实现学籍的查看,首先通过name1将学号传入,然后用sql语句查询,查询结果通过textbox.text,lable.text等显示,其中学号不能更改,其他最初不能更改,在点击修改后方可更改,点击保存后保存,同时其中可以打开窗体加载图像文件。

DataSet ds;
SqlDataAdapter da;
private void Fxueji_Load(object sender, EventArgs e)
        {
            textBox1.Enabled = false ;
            textBox2.Enabled = false;
            textBox3.Enabled = false;
            comboBox1.Enabled = false;
            pictureBox1.Enabled = false;
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            label9.Text = Fxuejilogin.name1;
            string s_ID;
            s_ID = Fxuejilogin.name1;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=localhost;Initial Catalog=student;Integrated Security=SSPI;";
            con.Open();
            string sqlstring = "select * from studentinfo where student_ID='" + s_ID + "'";
            SqlCommand command = new SqlCommand(sqlstring, con);
            da = new SqlDataAdapter(command);
            SqlCommandBuilder sb = new SqlCommandBuilder(da);
            da.DeleteCommand = sb.GetDeleteCommand();
            da.InsertCommand = sb.GetInsertCommand();
            da.UpdateCommand = sb.GetUpdateCommand();
            ds = new DataSet();
            da.Fill(ds,"studentinfo");
            this.textBox1.DataBindings.Add("text", ds, "studentinfo.student_Name", true);
            this.comboBox1.DataBindings.Add("text", ds, "studentinfo.student_Sex", true);
            this.dateTimePicker1.DataBindings.Add("text", ds, "studentinfo.student_Bir", true);
            this.label11.DataBindings.Add("text", ds, "studentinfo.major", true);
            this.dateTimePicker2.DataBindings.Add("text", ds, "studentinfo.student_Rdate", true);
            this.textBox2.DataBindings.Add("text", ds, "studentinfo.address", true);
            this.textBox3.DataBindings.Add("text", ds, "studentinfo.memment", true);
            this.pictureBox1.DataBindings.Add("image", ds, "studentinfo.student_Photo", true);
            bmb = this.BindingContext[this.ds,"studentinfo"]; 
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = true;
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            comboBox1.Enabled = true;
            pictureBox1.Enabled = true;
            dateTimePicker1.Enabled = true;
            dateTimePicker2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bmb.EndCurrentEdit();
            da.Update(ds,"studentinfo");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ds.RejectChanges();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog ();
            dlg.Filter = "JPEG文件(*.jpg)|*.jpg|BMP文件(*.bmp)|*.bmp";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Bitmap bp = new Bitmap(dlg.FileName );
                this.pictureBox1.Image = bp;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.pictureBox1.Image = null;
        }BindingManagerBase bmb;

4.班级介绍窗体

利用dataGridView绑定数据库中的表格,不仅能够查询,同时能够点击保存按钮进行插入和删除操作

private void Fbanjieshao_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=localhost;Initial Catalog=student;Integrated Security=SSPI;";
            con.Open();
            string ssql = "select * from classinfo";
            SqlCommand cmd = new SqlCommand(ssql, con );
            da = new SqlDataAdapter(cmd);
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            SqlCommandBuilder sb = new SqlCommandBuilder(da);
            da.DeleteCommand = sb.GetDeleteCommand();
            da.InsertCommand = sb.GetInsertCommand();
            da.UpdateCommand = sb.GetUpdateCommand();
            ds = new DataSet();
            da.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            da.Update(ds);
        }

5.班级管理窗体

首先需要用登录界面进行登录,登录种类为教师,type2因已经介绍过,不赘述。

打开班级管理窗体,原理与前一个相同。

private void button1_Click(object sender, EventArgs e)
        {
            da.Update(ds, "classinfo");

        }

        private void Fkecheng_Load(object sender, EventArgs e)
        {
            textBox2.Text = Fbanlog.name2;
            string st;
            st = Fbanlog.name2;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=localhost;Initial Catalog=student;Integrated Security=SSPI;";
            con.Open();
            string sqlstring = "select * from classinfo where director='" + st + "'";
            SqlCommand command = new SqlCommand(sqlstring, con);
            da = new SqlDataAdapter(command);
            SqlCommandBuilder sb = new SqlCommandBuilder(da);
            da.DeleteCommand = sb.GetDeleteCommand();
            da.InsertCommand = sb.GetInsertCommand();
            da.UpdateCommand = sb.GetUpdateCommand();
            ds = new DataSet();
            da.Fill(ds, "classinfo");
            this.textBox1.DataBindings.Add("text", ds, "classinfo.class_No", true);
            this.comboBox1.DataBindings.Add("text", ds, "classinfo.major", true);
            this.textBox3.DataBindings.Add("text", ds, "classinfo.class_Rome_No", true);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = true;
            comboBox1.Enabled =true ;
            textBox3.Enabled =true ;
            
        }

6.课程介绍窗体

新加内容,能够通过按钮进行记录的上翻与下翻

DataSet ds;
SqlDataAdapter da;
BindingManagerBase bmb;
private void Fclass_Load(object sender, EventArgs e)
        {
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            textBox3.Enabled = false;
            textBox4.Enabled = false;
            textBox5.Enabled = false;
            string s_ID;
            s_ID = Fxuejilogin.name1;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=localhost;Initial Catalog=student;Integrated Security=SSPI;";
            con.Open();
            SqlCommand command = new SqlCommand("select * from courseinfo", con);
            da = new SqlDataAdapter(command);
            SqlCommandBuilder sb = new SqlCommandBuilder(da);
            da.DeleteCommand = sb.GetDeleteCommand();
            da.InsertCommand = sb.GetInsertCommand();
            da.UpdateCommand = sb.GetUpdateCommand();
            ds = new DataSet();
            da.Fill(ds,"courseinfo");
            

            this.textBox1.DataBindings.Add("Text", ds, "courseinfo.course_ID");
            this.textBox2.DataBindings.Add("text", ds, "courseinfo.course_Name");
            this.textBox3.DataBindings.Add("text", ds, "courseinfo.course_Type");
            this.textBox4.DataBindings.Add("text", ds, "courseinfo.major", true);
            this.textBox5.DataBindings.Add("text", ds, "courseinfo.course_Des", true);
            bmb=this.BindingContext[this.ds, "courseinfo"];
            bmb.PositionChanged += new EventHandler(bmb_PositionChanged);
            bindingNavigatorMovePreviousItem.Text = (bmb.Position + 1).ToString();
            bindingNavigatorCountItem.Text = bmb.Count.ToString ();


        }
        void bmb_PositionChanged(object sender,EventArgs e)
        {
            bindingNavigatorMovePreviousItem.Text = (bmb.Position + 1).ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (bmb.Count != 0 && bmb.Position > 0)
                bmb.Position --;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (bmb.Count != 0 && bmb.Position <bmb.Count -1)
                bmb.Position++;
        }

7.课程管理系统

private void Fcourse_Load(object sender, EventArgs e)
        {
            string s_ID;
            s_ID = Fxuejilogin.name1;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=localhost;Initial Catalog=student;Integrated Security=SSPI;";
            con.Open();
            SqlCommand command = new SqlCommand("select * from courseinfo", con);
            da = new SqlDataAdapter(command);
            SqlCommandBuilder sb = new SqlCommandBuilder(da);
            da.DeleteCommand = sb.GetDeleteCommand();
            da.InsertCommand = sb.GetInsertCommand();
            da.UpdateCommand = sb.GetUpdateCommand();
            ds = new DataSet();
            da.Fill(ds, "courseinfo");


            this.textBox1.DataBindings.Add("Text", ds, "courseinfo.course_ID");
            this.textBox2.DataBindings.Add("text", ds, "courseinfo.course_Name");
            this.textBox3.DataBindings.Add("text", ds, "courseinfo.course_Type");
            this.textBox5.DataBindings.Add("text", ds, "courseinfo.teacher_Name", true);
            this.listBox1 .DataBindings.Add("text", ds, "courseinfo.course_Des", true);
            this.textBox4.DataBindings.Add("text", ds, "courseinfo.major");
            bmb = this.BindingContext[this.ds, "courseinfo"];
            bmb.PositionChanged += new EventHandler(bmb_PositionChanged);
            label6.Text = (bmb.Position + 1).ToString()+"/"+bmb.Count;
            
        }
        void bmb_PositionChanged(object sender, EventArgs e)
        {
            label6.Text = (bmb.Position + 1).ToString()+"/" + bmb.Count;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (bmb.Count != 0 && bmb.Position > 0)
                bmb.Position--;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (bmb.Count != 0 && bmb.Position < bmb.Count - 1)
                bmb.Position++;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(bmb.Count != 0)
            {
                bmb.Position = 0;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (bmb.Count != 0)
                bmb.Position = bmb.Count - 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = true;
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            textBox5.Enabled = true;
            listBox1.Enabled = true;
        }


9.用户管理

参考博客TextBox控件的DataBindings属性

链接:https://www.cnblogs.com/dotnet261010/p/6731126.html

参考书籍:龚雄涛,胡昌杰,基于C#的SQLS应用技术,西安电子科技大学出版社



展开阅读全文

没有更多推荐了,返回首页