现在的位置: 首页 > 综合 > 正文

学生管理系统

2013年01月27日 ⁄ 综合 ⁄ 共 5427字 ⁄ 字号 评论关闭

核心代码:

登陆代码:

protected void Button1_Click(object sender, EventArgs e)

    {

        string name = TextBox1.Text.Trim();

        string psw = TextBox2.Text.Trim();

        OleDbConnection conn = new OleDbConnection();

        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\专业知识学习\ASP.NET\学生管理系统\students.mdb";

        conn.Open();

        OleDbCommand cmd = new OleDbCommand();

        cmd.Connection = conn;

        int i = DropDownList1.SelectedIndex;

        string sql="";

        switch(i)

        {

            case 0: sql="select * from student"; break;

            case 1: sql="select * from teacher"; break;

            case 2: sql="select * from menager"; break;

        }

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = sql;

        OleDbDataReader dr = cmd.ExecuteReader();

        while (dr.Read())

        {

            if (dr[0].ToString() == name && dr[3].ToString()==psw)

            {

                this.Session.Add("id", dr[0]);

                this.Session.Add("username", dr[1]);

                this.Session.Add("sex", dr[2]);

                if (i == 0)

                {

                    this.Session.Add("class", dr[4]);

                    this.Response.Redirect("main.aspx", true);

                }

                else if (i == 1)

                {

                    this.Response.Redirect("teacher.aspx",true);

                }

                else {

                    this.Response.Redirect("admin.aspx", true);

                }

                dr.Close();

                conn.Close();

            }

        }

        this.Response.Redirect("error.aspx", true);

        dr.Close();

        conn.Close();

}

 

学生查询课程代码:

protected void Page_Load(object sender, EventArgs e)

    {

        Label1.Text = (string)this.Session["id"];

        if (Label1.Text == "") this.Response.Redirect("login_error.aspx", true);

        Label2.Text = (string)this.Session["username"];

        Label3.Text = (string)this.Session["class"];

        AccessDataSource1.SelectCommand = "SELECT st_co.courseid, course.coursename, st_co.grade FROM (st_co INNER JOIN course ON st_co.courseid = course.courseid) WHERE (st_co.grade IS NOT NULL) AND (st_co.studentid = @id)";

        AccessDataSource1.SelectParameters.Add("@id", Label1.Text);

 

        float sum=0;

        int count=GridView1.Rows.Count;

        for (int i = 0; i < count;i++ )

            sum=Convert.ToSingle(GridView1.Rows[i].Cells[2].Text)+sum;

        sum=(sum / count-50)/10;

        if (sum > 0)

            Label4.Text = sum.ToString();

        else

            Label4.Text = "0";

    }

增加老师用户代码:

string psw = Label1.Text;

        OleDbConnection conn = new OleDbConnection();

        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\专业知识学习\ASP.NET\学生管理系统\students.mdb";

        conn.Open();

        OleDbCommand cmd = new OleDbCommand();

        cmd.Connection = conn;

        string sql = "insert into teacher values('" +Label1.Text+ "','" +Label2.Text+ "','"+Label3.Text+"','"+psw+"')";

        cmd.CommandText = sql;

        cmd.CommandType = CommandType.Text;

        OleDbDataReader dr = cmd.ExecuteReader();

        dr.Close();

        conn.Close();

删除教师代码:

protected void Button1_Click(object sender, EventArgs e)

    {

        OleDbConnection conn = new OleDbConnection();

        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\专业知识学习\ASP.NET\学生管理系统\students.mdb";

        conn.Open();

        OleDbCommand cmd = new OleDbCommand();

        cmd.Connection = conn;

        string sql = "delete from teacher where id='" +teacherid+"'";

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = sql;

        OleDbDataReader dr = cmd.ExecuteReader();

        dr.Close();

        conn.Close();

              

        this.Response.Redirect("admin.aspx", true);

    }

增加课程代码:

protected void Button1_Click(object sender, EventArgs e)

    {

        string teacher_id, course_id;

        teacher_id = (string)this.Session["teacher_id"];

        course_id = (string)this.Session["course_id"];

        OleDbConnection conn = new OleDbConnection();

        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\专业知识学习\ASP.NET\学生管理系统\students.mdb";

        conn.Open();

        OleDbCommand cmd = new OleDbCommand();

        cmd.Connection = conn;

        string sql = "insert into te_co(teacherid,courseid) values('"+teacher_id+"','"+course_id+"')";

        cmd.CommandText = sql;

        cmd.CommandType = CommandType.Text;

        OleDbDataReader dr= cmd.ExecuteReader();

        dr.Close();

        conn.Close();

        this.Response.Redirect("admin_co.aspx", true);

    }

录入成绩代码:

protected void Button1_Click(object sender, EventArgs e)

    {

        if (TextBox1.Text != "")

        {

            OleDbConnection conn = new OleDbConnection();

            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\专业知识学习\ASP.NET\学生管理系统\students.mdb";

            conn.Open();

            OleDbCommand cmd = new OleDbCommand();

            cmd.Connection = conn;

            string sql = "update st_co set grade='"+TextBox1.Text+"' where studentid='"+Label3.Text+"' and courseid='"+Label1.Text+"'";

            cmd.CommandText = sql;

            cmd.CommandType = CommandType.Text;

            OleDbDataReader dr = cmd.ExecuteReader();

            dr.Close();

            conn.Close();

 

            this.Response.Redirect("teacher_lurugrade.aspx", true);

        }

    }

 


实验总结:

通过本次实验,我学会了很多。尤其是控件的使用,gridview控件的功能十分的强大,本实验的主要操作是使用这个控件。本次用的数据库是ACCESS,通过这次实验,我发觉数据库的设计关系到整个项目。数据库设计得好,程序就容易实现,不大会出现异常。除了gridview绑定数据源,其他的连接数据库都是用代码实现的,其实原理都差不多,删除,插入,查询,更新等操作,也是用代码实现。这次实验也帮我复习了一次SQL的语法。

       数据库的输入限制用了正则表达式来实现,大大地提高了数据库的质量。

                                                                                                                                                      2007年7月22日

posted on 2007-07-22 12:40 endy008 阅读(1) 评论(0)  编辑 收藏 引用

抱歉!评论已关闭.