校园情趣之管理

                                MySchool管理系统

1.FrmLogin窗体(登陆窗体)

 

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 MySchool
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //验证用户名和密码是否匹配
            //准备连接字符串
            string str = "data source=.;initial catalog=Myschool;uid=sa;pwd=";
            //准备连接对象
            SqlConnection con = new SqlConnection(str);
            //准备发送的SQL语句
             string sql = "select count(1) from Student where studentName='"+txtUserName.Text+"' and loginPwd='"+txtPwd.Text+"'";
            //准备命令对象
            SqlCommand cmd = new SqlCommand(sql,con);
            //try catch 捕获异常处理
            try
            {
                con.Open();
                //执行命令
                int count = Convert.ToInt32(cmd.ExecuteScalar());
                if (count > 0)
                {
                  //登陆成功 进入管理员界面
                    this.Hide();
                    FrmAdmin frm = new FrmAdmin();
                    //在主窗体呈现在用户肉眼之前 悄悄改变用户名文本框中的值
                    frm.name = txtUserName.Text;
                    frm.Show();
                    //Tool是自定义的一个工具类
                    Tool.pwd = txtPwd.Text;
                    Tool.stuname = txtUserName.Text;
                }
                else
                {
                    MessageBox.Show("登录失败!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("连接失败!");
            }
            finally
            {
                con.Close();
            }
        }
        //Load里面的代码在被用户看到之前就已经被执行了
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            txtUserName.Text = "张玲";
            txtPwd.Text = "000001";

        }

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

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

2.登陆后进入的管理员主窗体(FrmAdmin)

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 MySchool
{
    public partial class FrmAdmin : Form
    {
        public FrmAdmin()
        {
            InitializeComponent();
        }
        //定义一个公有变量保存登陆的用户名
        public string name;
        private void FrmAdmin_Load(object sender, EventArgs e)
        {
            //在顶部显示欢迎用户文字
            this.Text = this.Text + "                欢迎您"+name;
        }

        private void FrmAdmin_FormClosing(object sender, FormClosingEventArgs e)
        {
            //点击关闭 关闭整个程序进程
            Application.Exit();
        }

        private void toolStripDropDownButton1_Click(object sender, EventArgs e)
        {
            // 为AddStudent窗体设置MDI
            FrmAddStudemt frm = new FrmAddStudemt();
            frm.MdiParent = this;
            frm.Show();
        }

        private void 编辑学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 按姓名模糊查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //为姓名模糊查询设置MDI
            FrmSearchByStuName frm = new FrmSearchByStuName();
            frm.MdiParent = this;
            frm.Show();
        }

        private void tsmiselectinfoByAge_Click(object sender, EventArgs e)
        {
            //为根据学生年龄查询出符合的学生信息
            FrmSelectByAge frm = new FrmSelectByAge();
            frm.MdiParent = this;
            frm.Show();
        }
        //点击新建学生信息按钮触发的事件
        private void tsmiaddstu_Click(object sender, EventArgs e)
        {
            FrmAddStudemt frm = new FrmAddStudemt();
            frm.MdiParent = this;
            frm.Show();
        }
         //点击按学生姓名查询学生信息按钮触发的事件
        private void tsmiSelectByName_Click(object sender, EventArgs e)
        {
            FrmSearchByStuName frm = new FrmSearchByStuName();
            frm.MdiParent = this;
            frm.Show();
        }

        private void tsmiHelp_Click(object sender, EventArgs e)
        {

        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //为关于窗体设置MDI
            FrmChangePics frm = new FrmChangePics();
            frm.MdiParent = this;                
            frm.Show();
        }

        private void tsmizhanghuguanli_Click(object sender, EventArgs e)
        {

        }

        private void tsmixiugaiPwd_Click(object sender, EventArgs e)
        {
            //为修改密码设置MDI
            FrmModifyPwd frm = new FrmModifyPwd();
            frm.MdiParent = this;
            frm.Show();
        }

        private void tsmiEditStuInfo_Click(object sender, EventArgs e)
        {

        }
    }
}

3.新建用户窗体(FrmAddStudent窗体)

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;
using System.Data.SqlClient;

namespace MySchool
{
    public partial class FrmAddStudemt : Form
    {
        public FrmAddStudemt()
        {
            InitializeComponent();
        }
        public int stuno;
        //加载所有年级信息
        public void LoadGradeInfo()
        {
            //拿到所有年级的名称
            string str = "data source=.;initial catalog=MySchool;uid=sa;pwd=";
            SqlConnection con = new SqlConnection(str);
            string sql = "select gradeName from grade";
            SqlCommand cmd = new SqlCommand(sql, con);

            try
            {
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr != null)
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            string name = dr["gradeName"].ToString();
                            //为年级赋初值
                            cbbStuGrade.Items.Add(name);
                        }
                        //循环结束关闭读取器
                        dr.Close();
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("连接失败!");
            }
            finally
            {
                con.Close();
            }
        }
        public int gradeid;
        //根据学号加载学生的其他信息(用于右键菜单时修改指定的学生基本信息)
        public void LoadInfoByStuNo()
        {
            string str = "data source=.;initial catalog=MySchool;uid=sa;pwd=";
            SqlConnection con = new SqlConnection(str);
            string sql = "select * from student where studentno="+stuno+"";
            SqlCommand cmd = new SqlCommand(sql, con);
            try
            {
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if(dr!=null)
                {
                  if(dr.HasRows)
                  {
                    while(dr.Read())
                    {
                        string stuname=dr["studentname"].ToString();
                        txtStuName.Text = stuname;
                        string stuaddress=dr["address"].ToString();
                        txtStuAddress.Text = stuaddress;
                        string stuphone=dr["phone"].ToString();
                        txtStuPhone.Text = stuphone;
                        string email=dr["Email"].ToString();
                        txtStuEmail.Text = email;
                        gradeid = Convert.ToInt32(dr["gradeid"]);
                        string gradename = GetNameByGradeId();
                        cbbStuGrade.Text = gradename;
                        DateTime dt = Convert.ToDateTime(dr["birthday"]);
                        dtpBirth.Value = dt;
                        string sex = Convert.ToString(dr["gender"]);
                        if(sex.Equals("0"))
                        {
                            rbFeMale.Checked = true;
                        }
                        else if(sex.Equals("1"))
                        {
                            rbMale.Checked = true;
                        }
                                             
                    }
                    dr.Close();

                  }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        
        }

        //根据年级编号拿到年级名称
        public string GetNameByGradeId()
        {
            string str = "data source=.;initial catalog=Myschool;uid=sa;pwd=";
            SqlConnection con = new SqlConnection(str);

            string sql = "select gradename from grade where gradeid=" + gradeid + "";
            SqlCommand cmd = new SqlCommand(sql,con);
            string gradename="";
            try
            {
                con.Open();
                gradename = Convert.ToString(cmd.ExecuteScalar());
            }
            catch (Exception)
            {
                MessageBox.Show("错误!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                con.Close();
            }
            return gradename;
        }

        private void FrmAddStudemt_Load(object sender, EventArgs e)
        {
            LoadGradeInfo();
            //拿到选中的学生的学号
            txtBStuNo.Text = stuno.ToString();
            LoadInfoByStuNo();
        }

        private void gbzhuce_Enter(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void txtBAginPwd_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtBStuNo_TextChanged(object sender, EventArgs e)
        {

        }

        private void gbBaseInfo_Enter(object sender, EventArgs e)
        {

        }
        //添加学生信息
        public void Addinfo()
        {
            //添加学生 
            string pwd;
            string pwdagin;

            pwd = txtBStuPwd.Text;//密码
            pwdagin = txtBAginPwd.Text;//确认密码

            string name = txtStuName.Text;//姓名
            //获取性别
            string sex = String.Empty;
            if (rbMale.Checked)
            {
                sex = "1";
            }
            else
            {
                sex = "0";
            }
            //根据所选的年级名称拿到相应的年纪编号 并提交到数据库
            int gid = GetIdByName();
            //获取电话
            string phone = txtStuPhone.Text;
            //获取地址
            string address = txtStuAddress.Text;
            //获取出生日期
            DateTime birthday = dtpBirth.Value;
            //获取Email
            string email = txtStuEmail.Text;

            //将获取到的数据插入到数据库中
            //StudentNo, LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email
            string str = "data source=.;initial catalog=MySchool;uid=sa;pwd=";
            SqlConnection con = new SqlConnection(str);
            string sql = @"insert into Student(LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email)
                          values('" + pwd + "','" + name + "','" + sex + "'," + gid + ",'" + phone + "','" + address + "','" + birthday + "','" + email + "')";
            SqlCommand cmd = new SqlCommand(sql, con);
            try
            {
                con.Open();
                int count = Convert.ToInt32(cmd.ExecuteNonQuery());
                if (count > 0)
                {
                    MessageBox.Show("添加成功!");
                }
                else
                {
                    MessageBox.Show("添加失败!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("网络异常!");
            }
            finally
            {
                con.Close();
            }

        }
        //点击保存按钮时执行相应的操作
        private void btnSave_Click(object sender, EventArgs e)
        {
            //学号文本框为空字符串时执行添加操作
            if (txtBStuNo == null)
            {
                Addinfo();
            }
                //否则执行右键菜单的额修改操作
            else
            { 
            
            }
        }

        //该方法将年级名称转化成年级编号
        private int GetIdByName()
        {
            string str = "data source=.;initial catalog=MySchool;uid=sa;pwd=";
            SqlConnection con = new SqlConnection(str);
            string sql = "select gradeid from grade where gradeName='" + cbbStuGrade .Text+ "'";
            SqlCommand cmd = new SqlCommand(sql, con);
            int gid = 0;
            try
            {
                con.Open();
                gid = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception)
            {
                MessageBox.Show("网络异常!");
            }
            finally
            {
                con.Close();
            }
            return gid;
        }

      
    }
}

4.根据学生姓名模糊查询学生信息

  

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 MySchool
{
    public partial class FrmSearchByStuName : Form
    {
        public FrmSearchByStuName()
        {
            InitializeComponent();
        }
        //加载所有DB数据到ListView  此方法接受相应按钮的SQL语句
        public void LoadDataToListViewFromDB(string sql)
        {
            string str = "data source=.;initial catalog=MySchool;uid=sa;pwd=";
            SqlConnection con = new SqlConnection(str);
            
            SqlCommand cmd = new SqlCommand(sql, con);
            try
            {
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr != null)
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            //形成变量
                            //每从DB中读取一条数据,需要形成一个ListView对象,代表的是ListView中的一行
                            int stuNo = Convert.ToInt32(dr["studentNO"]);
                            string stuname = Convert.ToString(dr["studentName"]);
                            string stusex = dr["gender"].ToString();
                            string gradename = dr["gradename"].ToString();
                            ListViewItem lvItem = new ListViewItem(stuNo.ToString());
                            lvItem.SubItems.Add(stuname);
                            lvItem.SubItems.Add(stusex);
                            lvItem.SubItems.Add(gradename);
                            //让lvItem和ListView关联
                            lvList.Items.Add(lvItem);
                        }
                        dr.Close();
                    }
                }

            }
            catch (Exception)
            {
                MessageBox.Show("网络异常!");
            }
            finally
            {
                con.Close();
            }

        }
          //Load中的代码在窗体展现在用户肉眼之前应经被执行  即将所有学生信息打印出来
        private void FrmSearchByStuName_Load(object sender, EventArgs e)
        {
            string sql = @"select studentno,studentname,gender,gradename from grade,student where grade.GradeId=Student.GradeId";
            LoadDataToListViewFromDB(sql);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }
        //点击查询按钮时触发的事件
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //查询数据前,清空ListView中的数据
            lvList.Items.Clear();
            string sql = @"select studentno,studentname,gender,gradename
                            from student,grade
                             where student.gradeid=grade.gradeid and studentname like '%"+txtName.Text+"%'";
            LoadDataToListViewFromDB(sql);
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            string sql = @"select studentno,studentname,gender,gradename
                           from student,grade
                            where student.gradeid=grade.gradeid";
            LoadDataToListViewFromDB(sql);
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void 修改基本信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //获取用户选择的学生号
            int stuno = Convert.ToInt32(lvList.SelectedItems[0].Text);
            FrmAddStudemt frm = new FrmAddStudemt();
            //在修改窗体出现之前,悄悄给学号文本框赋上用户选择修改的学生学号
            frm.stuno = stuno;
            frm.Show();
           // MessageBox.Show(stuno.ToString());
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }
    }
}

 

5.帮助中的关于窗体

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 MySchool
{
    public partial class FrmChangePics : Form
    {
        public FrmChangePics()
        {
            InitializeComponent();
        }
        //定义一个变量保存图片索引值
        private int index = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (index < imgList.Images.Count - 1)
            {
                index++;
            }
            else
            {
                index = 0;
            }
            picImage.Image = imgList.Images[index];

        }

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

        private void FrmChangePics_Load(object sender, EventArgs e)
        {

        }
    }
}

关于窗体设计,请听下回分解

 

你可能感兴趣的:(校园情趣之管理)