教务系统——班级管理——全部代码

来源:互联网 发布:obs视频录制软件 编辑:程序博客网 时间:2024/05/01 11:38

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection();
    //public void lianjie()
    //{
    //    conn.ConnectionString = "Data Source=.;Initial Catalog=TeachingSystem;Integrated Security=True";
    //    conn.Open();
    //}
    public string updateclass( string classid, string classname, string departmentname, string teacherid )
    {
        //SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.;Initial Catalog=TeachingSystem;Integrated Security=True";
        conn.Open();
            string str=null;
            string sqlstr = "update TS_class set TS_Class_name=@classname,TS_Class_department=@departmentname,TS_Class_teacherid=@teacherid where TS_Class_id=@classid";
            SqlCommand comm = new SqlCommand(sqlstr, conn);
            try
            {
                comm.Parameters.Add(new SqlParameter("@classid", SqlDbType.VarChar, 50));
                comm.Parameters.Add(new SqlParameter("@classname", SqlDbType.VarChar, 50));
                comm.Parameters.Add(new SqlParameter("@departmentname", SqlDbType.VarChar , 50));
                comm.Parameters.Add(new SqlParameter("@teacherid", SqlDbType.VarChar , 50));


                comm.Parameters["@classid"].Value = classid;
                comm.Parameters["@classname"].Value =classname ;
                comm.Parameters["@departmentname"].Value = departmentname ;
                comm.Parameters["@teacherid"].Value =teacherid ;

                //comm.Connection.Open();
                comm.ExecuteNonQuery();
         
                str = "更新成功";
             
            }
            catch (Exception ex)
            {
              
                str = ex.Message;
            }
            comm.Connection.Close();
          
            return str;      

    }
    public DataSet Query(string selectstr)
    {
        DataSet ds = new DataSet();
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter(selectstr, conn);
            adp.Fill(ds);

        }
        catch (Exception)
        {

        }
        return ds;

    }
    public int NonQuery(string sqlstr)
    {
        int i = -1;
        SqlCommand comm = new SqlCommand(sqlstr, conn);
        //comm.Connection.Open();
        try
        {
            i = comm.ExecuteNonQuery();
        }
        catch (Exception)
        {

        }
        comm.Connection.Close();
        return i;
    }
    public void TS_BindGrid()
    {
        //lianjie();
        //SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.;Initial Catalog=TeachingSystem;Integrated Security=True";
        conn.Open();
        string sqlstr = "select * from TS_class";       
        DataSet ds = Query(sqlstr);
        conn.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }   
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {      
        GridView1.EditIndex = (int)e.NewEditIndex;
        TS_BindGrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //lianjie();

        string classid = GridView1.DataKeys[(int)e.RowIndex].Value.ToString();
        string classname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string departmentname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string teacherid = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        string str = updateclass( classid , classname, departmentname, teacherid);
        conn.Close();
        Label2.Text = str;
        GridView1.EditIndex = -1;
        TS_BindGrid();

       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {     

        //lianjie();
        //SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.;Initial Catalog=TeachingSystem;Integrated Security=True";
        conn.Open();
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;
        comm.CommandText = "select TS_Class_id,TS_Class_name,TS_department_name,TS_Teacher_name from TS_class,TS_department,TS_teacher where TS_class.TS_Class_department=TS_department.TS_department_id and TS_class.TS_Class_teacherid=TS_teacher.TS_Teacher_id and " + DropDownList1.SelectedValue + "='" + DropDownList2.Text + "'";
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = comm;
        sda.Fill(ds);
        conn.Close();      
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //lianjie();
        //SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.;Initial Catalog=TeachingSystem;Integrated Security=True";
        conn.Open();
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;
        comm.CommandText = "select TS_Class_id,TS_Class_name,TS_department_name,TS_Teacher_name from TS_class,TS_department,TS_teacher where TS_class.TS_Class_department=TS_department.TS_department_id and TS_class.TS_Class_teacherid=TS_teacher.TS_Teacher_id";
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = comm;
        sda.Fill(ds);       
        comm.ExecuteNonQuery();
        conn.Close();
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        TS_BindGrid();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
      
        if (DropDownList1.SelectedItem.Text == "班级")
        {
            DropDownList2.Items.Clear();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            //SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.;Initial Catalog=TeachingSystem;Integrated Security=True";
            conn.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandText = "select TS_Class_name from TS_class ";

            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = comm;
            sda.Fill(ds);
            conn.Close();
            dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DropDownList2.Items.Add((string)(dt.Rows[i][0]));
            }
        }
        if (DropDownList1.SelectedItem.Text == "系部")
        {
            DropDownList2.Items.Clear();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            //SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.;Initial Catalog=TeachingSystem;Integrated Security=True";
            conn.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandText = "select distinct TS_department_name from TS_department ";

            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = comm;
            sda.Fill(ds);
            conn.Close();
            dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DropDownList2.Items.Add((string)(dt.Rows[i][0]));
            }
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        conn.ConnectionString = "Data Source=.;Initial Catalog=TeachingSystem;Integrated Security=True";
        conn.Open();
        string sqlstr = "delete from TS_class where TS_Class_id='" + GridView1.DataKeys[(int)e.RowIndex].Value + "'";

        try
        {           
            NonQuery(sqlstr);
            Label2.Text = "删除成功!!";
        }
        catch (Exception ex)
        {
            Label2.Text = "删除失败" + ex.Message;
            Label2.Style["color"] = "red";
        }
        conn.Close();
        TS_BindGrid();
    }
}

原创粉丝点击