/// <summary> /// 判断学生id是否存在 /// </summary> /// <param name="_iStuId">学生id</param> /// <returns>存在返回 true 数据存储到m_oStudent中</returns> private bool JuageStuIdExit(int _iStuId) { bool bIsSucc = false; List <CStudentInfo> lstStudentInfo = new List <CStudentInfo>(); lstStudentInfo = m_oMysql.MysqlSelect("select * from student where id=" + _iStuId); if (lstStudentInfo.Count > 0) { m_oStudent = lstStudentInfo[0]; bIsSucc = true; } return(bIsSucc); }
/// <summary> /// 修改信息 /// </summary> /// <param name="_oStudentInfo">摇修改的学生信息</param> /// <returns></returns> private bool ModifyInfo(CStudentInfo _oStudentInfo, int _iStuId) { bool bIsSucc = false; if (_oStudentInfo != null) { //拼接插入sql语句 string sSql = "update student set math=" + _oStudentInfo.m_dbMath + "," + "chinese=" + _oStudentInfo.m_dbChinese + "," + "english=" + _oStudentInfo.m_dbEnglish + " where id=" + _iStuId; if (m_oMysql.MysqlInsert(sSql)) { bIsSucc = true; } } return(bIsSucc); }
/// <summary> /// mysql查询 /// </summary> /// <param name="_sSql">sql语句</param> public List <CStudentInfo> MysqlSelect(string _sSql) { List <CStudentInfo> lstStudentInfo = new List <CStudentInfo>(); MySqlCommand oMycmd = new MySqlCommand(_sSql, m_oMySqlConn); m_oSelectResult = oMycmd.ExecuteReader(); while (m_oSelectResult.Read()) { CStudentInfo oStu = new CStudentInfo(); oStu.m_iId = Convert.ToInt32(m_oSelectResult[0].ToString()); oStu.m_sName = m_oSelectResult[1].ToString(); oStu.m_iAge = Convert.ToInt32(m_oSelectResult[2].ToString()); oStu.m_iSex = Convert.ToInt32(m_oSelectResult[3].ToString()); oStu.m_dbMath = Convert.ToDouble(m_oSelectResult[4].ToString()); oStu.m_dbChinese = Convert.ToDouble(m_oSelectResult[5].ToString()); oStu.m_dbEnglish = Convert.ToDouble(m_oSelectResult[6].ToString()); lstStudentInfo.Add(oStu); } m_oSelectResult.Close(); return(lstStudentInfo); }
/// <summary> /// 插入信息 /// </summary> /// <param name="_oStudentInfo">要插入的学生信息</param> /// <returns></returns> private bool InsertInfo(CStudentInfo _oStudentInfo) { bool bIsSucc = false; if (_oStudentInfo != null) { //拼接插入sql语句 string sSql = "insert into student (name,age,sex,math,chinese,english) values (" + "\"" + _oStudentInfo.m_sName + "\"" + "," + _oStudentInfo.m_iAge + "," + _oStudentInfo.m_iSex + "," + _oStudentInfo.m_dbMath + "," + _oStudentInfo.m_dbChinese + "," + _oStudentInfo.m_dbEnglish + ")"; if (m_oMysql.MysqlInsert(sSql)) { bIsSucc = true; } } return(bIsSucc); }
/// <summary> /// 初始化 /// </summary> private void Init() { m_oStudent = new CStudentInfo(); m_lstStuInfo = new List <CStudentInfo>(); }
/// <summary> /// 添加用户按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_add_Click(object sender, EventArgs e) { //用来插入的学生对象 CStudentInfo oStudentInfo = new CStudentInfo(); if (this.add_name.Text == null || this.add_name.Text == "") { MessageBox.Show("请输入必须字段姓名", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); return; } //名称 oStudentInfo.m_sName = this.add_name.Text; //年龄 if (this.add_age.Text == null || this.add_age.Text == "") { } else { oStudentInfo.m_iAge = Convert.ToInt32(this.add_age.Text); } //数学成绩 if (this.add_math.Text == null || this.add_math.Text == "") { } else { oStudentInfo.m_dbMath = Convert.ToDouble(this.add_math.Text); } //语文成绩 if (this.add_chinese.Text == null || this.add_chinese.Text == "") { } else { oStudentInfo.m_dbEnglish = Convert.ToDouble(this.add_chinese.Text); } //英语成绩 if (this.add_english.Text == null || this.add_english.Text == "") { } else { oStudentInfo.m_dbEnglish = Convert.ToDouble(this.add_english.Text); } //性别 if (this.cb_nan.Checked) { oStudentInfo.m_iSex = 1; } else if (this.cb_nv.Checked) { oStudentInfo.m_iSex = 2; } //插入操作 if (this.InsertInfo(oStudentInfo)) { MessageBox.Show("插入学生 : " + oStudentInfo.m_sName + " 成功", "提示", MessageBoxButtons.OKCancel); //清空输入框信息 this.ClearEditInfo(); //更新listview控件,显示用户数据 this.ShowStudentInfo(); } else { MessageBox.Show("插入学生 : " + oStudentInfo.m_sName + " 失败", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); return; } }