//增加课程 public int AddCourse(Model.Course.Course objCourse, out string strErr) { int iRent = 0; strErr = ""; SqlCommand sqlCom = new SqlCommand(); string strSql = "insert into Course(CourseNo,CourseName,Points,Remark,Type)"; strSql += "values('" + objCourse.CourseNo + "','" + objCourse.CourseName + " ','" + objCourse.Points.ToString() + "' ,'" + objCourse.Remark + "',1)"; sqlCom.CommandText = strSql; sqlCom.Connection = sqlCon; try { sqlCon.Open(); sqlCom.ExecuteNonQuery(); iRent = 1; } catch (Exception exErr) { strErr = exErr.ToString(); iRent = 0; } finally { sqlCon.Close(); sqlCom.Dispose(); } return(iRent); }
//修改课程 public int ModifyCourse(string strCourseNumber, Model.Course.Course objCourse, out string strErr) { int iRent = 0; strErr = ""; SqlCommand sqlCom = new SqlCommand(); sqlCom.Connection = sqlCon; string strSql = "update Course set CourseNo='" + objCourse.CourseNo + "',CourseName='" + objCourse.CourseName + "', "; strSql += " Points='" + objCourse.Points.ToString() + "',Remark='" + objCourse.Remark + "' where CourseNo='" + strCourseNumber + "'"; sqlCom.CommandText = strSql; try { sqlCon.Open(); sqlCom.ExecuteNonQuery(); iRent = 1; } catch (Exception ex) { strErr = ex.ToString(); iRent = 0; } finally { sqlCon.Close(); sqlCom.Dispose(); } return(iRent); }
//添加成绩信息 private void btnSave_Click(object sender, EventArgs e) { string strErr = ""; Model.Class.Class objClass1 = new Model.Class.Class(); Model.Student.Student objStudent1 = new Model.Student.Student(); Model.Course.Course objCourse1 = new Model.Course.Course(); objClass1.ClassNo = cmbClassName.SelectedValue.ToString(); objStudent1.StuId = int.Parse(cmbStuNo.SelectedValue.ToString()); objCourse1.CourseId = int.Parse(cmbCourseName.SelectedValue.ToString()); string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["StudentManager"].ConnectionString; Manage.Score.Score objSco = new Manage.Score.Score(strConnection); int i = objSco.Judge(objStudent1, objCourse1); // 判断是否存在所选的成绩,如果为0,表示不存在记录,可以增加。为1表示存在记录,不能重复插入成绩。 if (i == 0) { Model.Score.Score objScore = new Model.Score.Score(); Model.Student.Student objStudent = new Model.Student.Student(); Model.Course.Course objCourse = new Model.Course.Course(); objScore.CourseId = int.Parse(cmbCourseName.SelectedValue.ToString()); objScore.StuId = int.Parse(cmbStuNo.SelectedValue.ToString()); objScore.score = float.Parse(nudScore.Value.ToString()); objScore.StuName = txtStuName.Text; int iRent = objSco.AddScore(objScore, out strErr); if (iRent == 0) { MessageBox.Show(strErr); return; } else { MessageBox.Show("添加成功"); this.Visible = false; BindClassId(); BindStuId(); BindCourseId(); } } else { MessageBox.Show("不能重复插入分数"); } }
//修改成绩信息 private void btnSave_Click(object sender, EventArgs e) { string strErr = ""; Model.Student.Student objStudent1 = new Model.Student.Student(); Model.Course.Course objCourse1 = new Model.Course.Course(); objStudent1.StuId = int.Parse(txtStuNo.ToString()); objCourse1.CourseId = int.Parse(txtCourseNo.ToString()); string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["StudentManager"].ConnectionString; Manage.Score.Score objSco = new Manage.Score.Score(strConnection); int i = objSco.Judge(objStudent1, objCourse1); //返回为0,表示不存在该成绩记录,不能修改。返回为1,表示存在记录,可以修改。 if (i == 1) { if (DialogResult.Yes == MessageBox.Show("真的要修改该成绩吗?", "确认修改", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { string strEr = ""; Model.Score.Score objScore = new Model.Score.Score(); Model.Student.Student objStudent = new Model.Student.Student(); Model.Course.Course objCourse = new Model.Course.Course(); objScore.CourseId = int.Parse(txtCourseNo.ToString()); objScore.StuId = int.Parse(txtStuNo.ToString()); objScore.score = float.Parse(nudScore.Value.ToString()); int iRent = objSco.ModifyScore(objScore, out strErr); if (iRent == 0) { MessageBox.Show(strEr); return; } else { MessageBox.Show("修改成功"); //BindDataGridView(); this.Visible = false; } } } else if (i == 0) { MessageBox.Show("当前要修改的成绩不存在,请确定后再修改"); } }
}//学生登陆后,绑定表里显示的数据 public int Judge(Model.Student.Student objStuId, Model.Course.Course objCourseId) { string strErr = ""; Model.Score.Score objScore = new Model.Score.Score(); DataSet ds = new DataSet(); string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["StudentManager"].ConnectionString; string strSql = "select Score from score where stuid='" + objStuId.StuId.ToString() + "'and CourseId='" + objCourseId.CourseId + "'and type =1"; SqlConnection sqlCon = new SqlConnection(); sqlCon.ConnectionString = strConnection; sqlCon.Open(); SqlCommand sqlCom = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(strSql, sqlCon); try { da.Fill(ds); } catch (Exception exce) { strErr = exce.ToString(); } finally { sqlCon.Close(); } if (ds.Tables[0].Rows.Count != 0) { return(1); } else { return(0); } }
public void LoadData() { string strErr = ""; Model.Course.Course objCourse = new Model.Course.Course(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["StudentManager"].ConnectionString; string strSql = "select * from Course where CourseNo ='" + strCourseNumber + "'"; SqlConnection conn = new SqlConnection(); conn.ConnectionString = strConnection; conn.Open(); SqlCommand sqlcomd = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(strSql, conn); try { da.Fill(ds); } catch (Exception ex) { strErr = ex.ToString(); } finally { conn.Close(); } DataRow dr = ds.Tables[0].Rows[0]; courseno = this.txtCourseNo1.Text = dr["CourseNo"].ToString(); courseno = courseno.Trim(); coursename = this.txtCourseName.Text = dr["CourseName"].ToString(); coursename = coursename.Trim(); this.txtPoints.Text = dr["Points"].ToString(); this.txtRemark.Text = dr["Remark"].ToString(); }
private void btnSubmit_Click(object sender, EventArgs e) { string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["StudentManager"].ConnectionString; Manage.Course.Course objManageCourse = new Manage.Course.Course(strConnection); string strErr = ""; Model.Course.Course objCourse = new Model.Course.Course(); objCourse.CourseNo = txtCourseNo1.Text.Replace("'", "''"); objCourse.CourseNo = objCourse.CourseNo.Trim(); objCourse.CourseName = txtCourseName.Text.Replace("'", "''"); objCourse.CourseName = objCourse.CourseName.Trim(); //objCourse.Points = float.Parse(this.txtPoints.Text.ToString().Replace("'", "''")); //objCourse.Points = objCourse.Points.Trim(); objCourse.Remark = txtRemark.Text.Replace("'", "''"); objCourse.Remark = objCourse.Remark.Trim(); if (this.txtPoints.Text != "") { try { objCourse.Points = float.Parse(this.txtPoints.Text.ToString().Replace("'", "''")); if (this.txtPoints.Text.IndexOf('.') == 0) { MessageBox.Show("小数点之前没有数字"); return; } if (this.txtPoints.Text.Length - this.txtPoints.Text.IndexOf('.') - 1 == 0) { MessageBox.Show("小数点后没有数字"); return; } else { objCourse.Points = float.Parse(this.txtPoints.Text.ToString().Replace("'", "''")); } if (objCourse.Points < 0) { MessageBox.Show("请输入正数"); return; } } catch { MessageBox.Show("请输入正确的学分"); this.txtPoints.Text = ""; this.txtPoints.Focus(); return; } } //objCourse.CourseNo = txtCourseNo.Text.Replace("'", "''"); //objCourse.CourseNo = objCourse.CourseNo.Trim(); //objCourse.CourseName = txtCourseName.Text.Replace("'", "''"); //objCourse.CourseName = objCourse.CourseName.Trim(); //objCourse.Points = float.Parse(this.txtPoints.Text.ToString().Replace("'", "''")); //objCourse.Points = objCourse.Points.Trim(); //objCourse.Remark = txtRemark.Text.Replace("'", "''"); //objCourse.Remark = objCourse.Remark.Trim(); if (txtCourseName.Text == "" || txtCourseNo1.Text == "") { MessageBox.Show("请将课程编号和名称填写完整"); } else if (courseno == objCourse.CourseNo && coursename == objCourse.CourseName) { int iRent = objManageCourse.ModifyCourse(strCourseNumber, objCourse, out strErr); if (iRent == 0) { MessageBox.Show(strErr); return; } else { MessageBox.Show("修改成功"); this.Visible = false; } } else { int iCourseNo1 = objManageCourse.JudgeCourseNo(objCourse.CourseNo, out strErr); int iCourseNo2 = objManageCourse.JudgeCourseName(objCourse.CourseName, out strErr); if (courseno != objCourse.CourseNo && coursename == objCourse.CourseName) { if (iCourseNo1 == 0) { int iRent = objManageCourse.ModifyCourse(strCourseNumber, objCourse, out strErr); if (iRent == 0) { MessageBox.Show(strErr); return; } else { MessageBox.Show("修改成功"); this.Visible = false; } } else { MessageBox.Show("当前课程编号已经存在,修改失败"); txtCourseNo.Text = courseno; } } else if (courseno == objCourse.CourseNo && coursename != objCourse.CourseName) { if (iCourseNo2 == 0) { int iRent = objManageCourse.ModifyCourse(strCourseNumber, objCourse, out strErr); if (iRent == 0) { MessageBox.Show(strErr); return; } else { MessageBox.Show("修改成功"); this.Visible = false; } } else { MessageBox.Show("当前课程名称已经存在,修改失败"); txtCourseNo.Text = courseno; } } else { if (iCourseNo1 == 0 && iCourseNo2 == 0) { int iRent = objManageCourse.ModifyCourse(strCourseNumber, objCourse, out strErr); if (iRent == 0) { MessageBox.Show(strErr); return; } else { MessageBox.Show("修改成功"); this.Visible = false; } } else { MessageBox.Show("当前课程编号和名称都已经存在,修改失败"); txtCourseNo.Text = courseno; } } } }