学生管理系统

1.登录窗体

基本控件:

label(标签控件)

主要属性:Image(在标签上显示的图像)

Text(在标签上显示的文本)

TextBox(文本框控件)

主要属性:PasswordChar(指示在作为密码框时,文本框中显示的字符,而不是实际输入的文本)

Button(按钮控件)

ComboBox(下拉框)属性:SelectedItem:获取当前选定的项

事件:Click(单击控件时发生)

复制代码
private void butStyle_Click(object sender, EventArgs e)
        {
            string str = "Data source=.;Initial catalog=Myschool;uid=sa";
            SqlConnection con = new SqlConnection(str);
            string sql = "select count(1) from student where studentName='" + txtUserName.Text + "' and LoginPwd='" + txtPwd.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, con);
            try
            {
                con.Open();
                int count = Convert.ToInt32(cmd.ExecuteScalar());
                if (count > 0)
                {
                  MessageBox.Show("登陆成功");
                    this.Hide();
                    FormMain frm = new FormMain();
                    frm.Show();
                }
            }
            catch (Exception)
            {

                MessageBox.Show("退出");
            }
            finally
            {
                con.Close();
            }
复制代码

Sender是事件源,表示发生了这个事件的对象,事件发生中,事件源就是按钮。

e是事件参数(EventArgs)对象,不同的事件会有不同的参数。

Close()方法是窗体类Form的一个方法,作用是关闭窗体。

2.Myschool管理员

01.给菜单栏中的“新增学生”菜单项添加事件处理程序,代码如下

private void 新增学生ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormStudent formStudent = new FormStudent();
            formStudent.Show();
        }

02.添加学生信息

复制代码
 public void Save()
        {
            //添加学生
            string pwd = txtpwd.Text;
            string stuname = textname.Text;
            //性别
            string stugender = string.Empty;
            if (radioman.Checked)
            {
                stugender = "1";
            }
            else
            {
                stugender = "0";
            }
            //下拉框绑定数据
            int gid = GeadIdName();
            //联系电话
            string StuPhone = textphone.Text;
            //地址
            string StuAddress = textAddress.Text;
            //日期
            DateTime dt = dateBirthday.Value;
            //邮箱
            string StuEmail = textEmail.Text;
            //LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email
            string sql = "insert into Student values('" + pwd + "','" + stuname + "','" + stugender + "'," + gid + ",'" + StuPhone + "','" + StuAddress + "','" + dt + "','" + StuEmail + "')";
            string str = "Data source=.;Initial catalog=Myschool;uid=sa;";
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open();
            int count = cmd.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("添加成功");                 
            }
            con.Close();
        }
复制代码

3.查询学生信息

复制代码
//查询学生信息
        public void LodaDataListView(string sql)
        {
            string str = "data source=.;initial catalog=Myschool;uid=sa;";
            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())
                        {
                            int stuNo = Convert.ToInt32(dr["studentNo"]);
                            //姓名
                            string stuname = Convert.ToString(dr["studentName"]);
                            //性别
                            string stuGender = Convert.ToString(dr["Gender"]);
                            //年级名次
                            string stuGname = Convert.ToString(dr["Gradename"]);
                            ListViewItem LvItem = new ListViewItem(stuNo.ToString());
                            LvItem.SubItems.Add(stuname);
                            LvItem.SubItems.Add(stuGender);
                            LvItem.SubItems.Add(stuGname);
                            //让lvItem和ListView关联
                            lvlist.Items.Add(LvItem);
                        }
                        dr.Close();
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.Close();
            }
复制代码

//窗体Load的事件中调用

private void Formselect_Load(object sender, EventArgs e)
        {
            string sql = "select StudentNO,StudentName,Gender,GradeName from Student,Grade where Student.GradeId=Grade.GradeId";
            LodaDataListView(sql);
        }

修改学生信息

复制代码
public void upatae()
        {
            //添加学生
            string pwd = txtpwd.Text;
            string stuname = textname.Text;
            //性别
            string stugender = string.Empty;
            if (radioman.Checked)
            {
                stugender = "1";
            }
            else
            {
                stugender = "0";
            }
            //下拉框绑定数据
            int gid = GeadIdName();
            //联系电话
            string StuPhone = textphone.Text;
            //地址
            string StuAddress = textAddress.Text;
            //日期
            DateTime dt = dateBirthday.Value;
            //邮箱
            string StuEmail = textEmail.Text;
            //LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email
            string sql = @"update Student set StudentName='" + stuname + "',Gender=" + stugender + ",GradeId='" + gid + "',phone='" + StuPhone + "',Address='" + StuAddress + "',Birthday='" + dt + "',Email='" + StuEmail
                + "' where studentNo='" + textNo.Text + "'";
            string str = "Data source=.;Initial catalog=Myschool;uid=sa;";
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open();
            int count = cmd.ExecuteNonQuery();
            if (count > 0)
            {
                frmselect.selectData();
                MessageBox.Show("修改成功");
            }
            con.Close();
        }
复制代码

//性别if判定:性别男为1 为女0.

posted @ 2017-11-28 10:00  维尼熊320  阅读(489)  评论(0编辑  收藏  举报