⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentzhuanyeke.aspx.cs

📁 学生网上选课系统,实现了学生的在线选课和教师课程的上传
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
    protected void Page_Load(object sender, EventArgs e)
    {
        //string conn = "Data Source=dell156;Initial Catalog=pengpeng;Integrated Security=True";
        string myCon = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
        conn = new SqlConnection(myCon);

        if (!this.IsPostBack)
        {
            BindGrid();
        }
        //////string sql2 = "select Elect.stuID,Elect.courceID,Elect.teaID,Cource.courceName,Teacher.teaName,Cource.courceTime,Cource.courceAddress,Cource.credit,Cource.xueshi,Cource.courceInfo FROM Elect INNER JOIN Cource ON Elect.courceID=Cource.courceID AND Elect.teaID=Cource.teaID INNER JOIN Teacher ON Elect.teaID=Teacher.teaID AND Cource.teaID=Teacher.teaID where (Elect.stuID=@stuID)";
        //////DataSet myds2 = new DataSet();
        //////SqlDataAdapter myda2 = new SqlDataAdapter(sql2, conn);
        //////myda.Fill(myds2);
        //////GridView2.DataSource = myds2;
        //////GridView2.DataBind();
        //stuID.Text = Session["userName"].ToString();
        //string sqlselect = "select stuID,stuName,stuClass from student where student.stuID='" + Session["userName"].ToString() + " '";
        //SqlCommand comm1 = new SqlCommand(sqlselect, conn);
        //conn.Open();
        //SqlDataReader dr = comm1.ExecuteReader();
        //if (dr.Read())
        //{
        //    stuName.Text = dr["stuName"].ToString();
        //    stuClass.Text = dr["stuClass"].ToString();
        //}
        //conn.Close();
    }
    public void Data_Page(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            DataBind();
        }
        catch { }
    }
    protected void BindGrid()
    {
        string sql = "SELECT Cource.courceName,type.typename, Teacher.teaName, Cource.courceTime, Cource.courceAddress,Cource.courceId,Cource.credit,Cource.xueshi,Cource.courcerenshu,Cource.renshu,Teacher.teaid  FROM Cource,Teacher,type where Cource.teaID=Teacher.teaID and type.typeID=Cource.typeID and type.typeID='2'";

        DataSet myds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter(sql, conn);
        myda.Fill(myds);
        GridView1.DataSource = myds;
        GridView1.DataBind();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "select")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            DataKey key = GridView1.DataKeys[index];
            string courceID = GridView1.DataKeys[index].Values[0].ToString().Trim();
            string teaID = GridView1.DataKeys[index].Values[1].ToString().Trim();
            //SqlConnection conn = new SqlConnection(connstr);
            // string connstr = "Data Source=dell156;Initial Catalog=pengpeng;Integrated Security=True";
            string userName = Session["userName"].ToString();
            //计算选课们数
            //string mysql1 = "select count(stuID) as num from elect Where stuID='" + userName + "'";
            //SqlCommand cm1 = new SqlCommand(mysql1, conn);
            //SqlDataReader dr1 = cm1.ExecuteReader();
            //dr1.Read();
            //int num = Convert.ToInt32(dr1["num"]);
            //dr1.Close();
            //计算已选人数
            string mysql11 = "select * from cource Where courceID='" + courceID + "'";
            SqlCommand cm11 = new SqlCommand(mysql11, conn);
            conn.Open();
            SqlDataReader dr11 = cm11.ExecuteReader();
            dr11.Read();
            int csum = Convert.ToInt32(dr11["renshu"]);
            int climit = Convert.ToInt32(dr11["courcerenshu"]);
            dr11.Close();
            conn.Close();

            //选课满人
            if (csum < climit)
            {
                //先判断该学生的选课门数是否少于3门
                //if (num < 3)
                //{
                //验证该学生是否重复选了同一门课程
                //string settings2 = Convert.ToString(ConfigurationManager.ConnectionStrings["ElectConnectionString"]);
                //SqlConnection cn2 = new SqlConnection(settings2);

                string mysql2 = "select * from elect Where stuID='" + userName + "' and courceID='" + courceID + "'";
                SqlCommand cm2 = new SqlCommand(mysql2, conn);
                conn.Open();
                SqlDataReader dr2 = cm2.ExecuteReader();

                //该学生没有重复选同一门课程,执行选课操作
                if (!dr2.Read())
                {
                    dr2.Close();
                    conn.Close();
                    //string settings3 = Convert.ToString(ConfigurationManager.ConnectionStrings["ElectConnectionString"]);
                    //SqlConnection cn3 = new SqlConnection(settings3);
                    conn.Open();
                    string mysql3 = "insert into Elect(stuID,courceID,teaID) values('" + userName + "','" + courceID + "','" + teaID + "')";
                    //string mysql3 = "insert into elect(s_id,c_id) values(";
                    //mysql3 = mysql3 + " '" + s_id + "'";
                    //mysql3 = mysql3 + ",'" + c_id + "')";
                    SqlCommand cm3 = new SqlCommand(mysql3, conn);
                    cm3.ExecuteNonQuery();

                    //选修人数加1
                    string mysql4 = "update cource set renshu=renshu+1 where courceID='" + courceID + "'";
                    SqlCommand cm4 = new SqlCommand(mysql4, conn);
                    cm4.ExecuteNonQuery();
                    Response.Write("<script>alert('选修成功。')</script>");


                    BindGrid();
                    conn.Close();
                }
                //该学生重复选了同一门课程
                else
                {
                    Response.Write("<script>alert('你已经选修了这门课程,不能重复选。')</script>");
                    return;
                }
                dr2.Close();
                //cn2.Close();
            }
            //else
            //{
            //    Response.Write("<script>alert('选课不能超过3门!')</script>");

            //}
        }
        else
        {
            Response.Write("<script>alert('选课人数已满,请选择其他课程!')</script>");
            //dr1.Close();
            //cn1.Close();
        }

    }


    //        string SqlStr = "insert into Elect(stuID,courceID,teaID) values('"+userName+"','" + courceID + "','" + teaID + "')";
    //       // string SqlStr = "update Cource set renshu=renshu+1 where courceID='"+courceID+"'";
    //        try
    //        {
    //            //SqlConnection conn = new SqlConnection(connstr);
    //            if (conn.State.ToString() == "Closed")          
    //                conn.Open();
    //            SqlCommand comm = new SqlCommand(SqlStr, conn);
    //            comm.ExecuteNonQuery();                         
    //            comm.Dispose();
    //            if (conn.State.ToString() == "Open")            
    //                conn.Close();
    //        }
    //        catch (Exception ex)                                
    //        {
    //            Response.Write("数据库错误,错误原因:" + ex.Message);
    //            Response.End();
    //        }
    //    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("elect.aspx");
    }

    protected void fanhui_Click(object sender, EventArgs e)
    {
        Response.Redirect("studentElect.aspx");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -