//删除记录 public static int delete(string sql) { SqlConnection conn = DBCon.getConnection(); SqlTransaction transaction = null; try { conn.Open(); transaction = conn.BeginTransaction(); SqlCommand com = new SqlCommand(sql, conn, transaction); int n = com.ExecuteNonQuery(); transaction.Commit(); return(n); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } finally { DBCon.CloseAll(conn); } }
protected void studentselect_Click(object sender, EventArgs e) { Session["selectstudentxh"] = Convert.ToInt32(studentselectcontent.Text.Trim()); string sql = "select * from vi_student where xh ='" + Session["selectstudentxh"] + "'"; SqlConnection conn = DBCon.getConnection(); SqlCommand com = new SqlCommand(sql, conn); SqlDataReader stu = null; try { conn.Open(); stu = com.ExecuteReader(); } catch (Exception ex) { Console.WriteLine(ex); } Session["selectstudentresultinfo"] = null; if (stu.Read()) { Session["selectstudentresultinfo"] += "学生信息:<br /><br /> 学号:" + stu["xh"] + " 姓名:" + stu["xm"] + " 性别:" + stu["xb"] + " 年龄:" + stu["nl"] + " 院系:" + stu["dept"] + "<br /><br />"; } else { ScriptManager.RegisterStartupScript(this.selectstudent_panel, this.GetType(), "updateScript", "alert('没有这个学生!')", true); return; } DBCon.CloseAll(conn, com); SqlDataSource6.SelectParameters["kcdh"].DefaultValue = Session["selectstudentxh"].ToString(); string sql2 = "select * from vi_study where xh ='" + Session["selectstudentxh"] + "'"; SqlConnection conn2 = DBCon.getConnection(); SqlCommand com2 = new SqlCommand(sql2, conn2); SqlDataReader stu2 = null; try { conn2.Open(); stu2 = com2.ExecuteReader(); } catch (Exception ex) { Console.WriteLine(ex); } int sum = 0; int count = 0;; int max = 0; int min = 100; while (stu2.Read()) { if (stu2["cj"] != System.DBNull.Value) { sum += Convert.ToInt32(stu2["cj"]); count++; if (max < Convert.ToInt32(stu2["cj"])) { max = Convert.ToInt32(stu2["cj"]); } if (min > Convert.ToInt32(stu2["cj"])) { min = Convert.ToInt32(stu2["cj"]); } } } DBCon.CloseAll(conn2, com2); if (sum != 0) { Session["selectstudentresultinfo"] += " 最高分:" + max + " 最低分:" + min + " 平均分:" + sum / count + "<br /><br />"; } else { Session["selectstudentresultinfo"] += " 暂无成绩<br /><br />"; } Session["selectstudentresultinfo"] += "<br /><br />详细成绩单:<br />"; SelectStudentListView.DataSource = null; SelectStudentListView.DataBind(); selectstudentresult_panel.Visible = true; }
protected void teacheredit_Click(object sender, EventArgs e) { teacher_panel.Visible = true; manageteacher_panel.Visible = false; editteacher_panel.Visible = true; int gh = Int32.Parse((sender as Button).CommandArgument.ToString()); string sql = "select * from tb_teacher where gh ='" + gh + "'"; SqlConnection conn = DBCon.getConnection(); SqlCommand com = new SqlCommand(sql, conn); SqlDataReader teaedit = null; try { conn.Open(); teaedit = com.ExecuteReader(); } catch (Exception ex) { Console.WriteLine(ex); } if (teaedit.Read()) { teacheredituser.Text = teaedit["gh"].ToString(); teachereditname.Text = teaedit["xm"].ToString(); teachereditage.Text = teaedit["gl"].ToString(); teacheredittitles.Text = teaedit["zc"].ToString(); teachereditsalary.Text = teaedit["jbgz"].ToString(); if (teaedit["xb"].ToString() == "女") { teachereditsex2.Checked = true; } teachereditbirth.Text = Convert.ToDateTime(teaedit["csrq"]).ToString("yyyy-MM-dd"); teachereditbirth.Attributes.Add("OnClick", "WdatePicker({startDate: '" + Convert.ToDateTime(teaedit["csrq"]).ToString("yyyy-MM-dd") + "'})"); string sql_dept = "select * from tb_department"; SqlConnection conn_dept = DBCon.getConnection(); SqlCommand com_dept = new SqlCommand(sql_dept, conn_dept); SqlDataReader dept = null; try { conn_dept.Open(); dept = com_dept.ExecuteReader(); } catch (Exception ex) { Console.WriteLine(ex); } this.teachereditdept.Items.Clear(); int i = 0; while (dept.Read()) { this.teachereditdept.Items.Add(dept["dna"].ToString()); this.teachereditdept.Items[i].Value = dept["dno"].ToString(); if (dept["dno"].ToString() == teaedit["dno"].ToString()) { this.teachereditdept.Items[i].Selected = true; } i++; } DBCon.CloseAll(conn_dept, com_dept); } DBCon.CloseAll(conn, com); }
protected void courseedit_Click(object sender, EventArgs e) { managecourse_panel.Visible = false; editcourse_panel.Visible = true; int kcdh = Int32.Parse((sender as Button).CommandArgument.ToString()); string sql = "select * from tb_course where kcdh ='" + kcdh + "'"; SqlConnection conn = DBCon.getConnection(); SqlCommand com = new SqlCommand(sql, conn); SqlDataReader course = null; try { conn.Open(); course = com.ExecuteReader(); } catch (Exception ex) { Console.WriteLine(ex); } if (course.Read()) { courseeditnum.Text = course["kcdh"].ToString(); courseeditname.Text = course["kcm"].ToString(); courseeditcount.Text = course["kss"].ToString(); if (course["bxk"].ToString() == "True") { courseeditisneed.ClearSelection(); courseeditisneed.Items[0].Selected = true; } else { courseeditisneed.ClearSelection(); courseeditisneed.Items[1].Selected = true; } courseeditcredit.ClearSelection(); courseeditcredit.Items[Convert.ToInt32(course["xf"].ToString()) - 1].Selected = true; string sql_tea = "select * from tb_teacher"; SqlConnection conn_tea = DBCon.getConnection(); SqlCommand com_tea = new SqlCommand(sql_tea, conn_tea); SqlDataReader tea = null; try { conn_tea.Open(); tea = com_tea.ExecuteReader(); } catch (Exception ex) { Console.WriteLine(ex); } this.courseeditteacher.Items.Clear(); int i = 0; while (tea.Read()) { this.courseeditteacher.Items.Add(tea["xm"].ToString()); this.courseeditteacher.Items[i].Value = tea["gh"].ToString(); if (tea["gh"].ToString() == course["gh"].ToString()) { this.courseeditteacher.Items[i].Selected = true; } i++; } DBCon.CloseAll(conn_tea, com_tea); } DBCon.CloseAll(conn, com); }