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.OleDb;

public partial class _Class : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btn_value.Text = "添加";
            if (Session["AdminLevel"] == null || Session["AdminLevel"].ToString() != "1")
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                string action = Request.QueryString["Action"];
                if (action == "Add")
                {
                    btn_value.Text = "添加";
                }
                else if (action == "Edit")
                {
                    btn_value.Text = "编辑";
                    GetClass();
                }
                else if (action == "Logout")
                {
                    Session.Abandon();
                    Response.Redirect("Default.aspx");
                }
                classlist();
            }
        }
    }
    public void GetClass()
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        myconn.Open();
        //创建查询语句
        string sql = "select * from BigClass Where BigClassID=" + id;
        //Response.Write(sql);
        //Response.End();

        //创建数据库命令
        OleDbCommand mycmd = new OleDbCommand(sql, myconn);
        OleDbDataReader result = mycmd.ExecuteReader();

        if (result.Read())
        {
            tbx_class.Text = result["BigClassName"].ToString();
            tbx_url.Text = result["BigClassURL"].ToString();
        }
        myconn.Close();
    }

    public void classlist()
    {
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        myconn.Open();
        //创建查询语句
        string sql = "select * from BigClass";

        //创建数据库命令
        OleDbCommand mycmd = new OleDbCommand(sql, myconn);
        OleDbDataReader result = mycmd.ExecuteReader();
        gv_class.DataSource = result;
        gv_class.DataBind();
        myconn.Close();
    }

    protected void btn_value_Click(object sender, EventArgs e)
    {
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        string action = Request.QueryString["Action"].ToString();
                    string classname = tbx_class.Text;
            string url = tbx_url.Text;

        //打开数据库连接
        myconn.Open();
        if (action == "Add")
        {
            string sql = "insert into BigClass (BigClassName,BigClassUrl) Values(" + "'" + classname + "'" + ",'" + url + "')";
            //创建数据库命令
            OleDbCommand mycmd = new OleDbCommand(sql, myconn);
            mycmd.ExecuteNonQuery();
            myconn.Close();
            message.Visible = true;
            message.Text = "添加成功";
            classlist();
        }
        else if (action == "Edit")
        {
            string sql = "Update BigClass Set BigClassName=" + "'" + classname + "'," + "BigClassUrl=" + "'" + url + "'";
            //创建数据库命令
            OleDbCommand mycmd = new OleDbCommand(sql, myconn);
            mycmd.ExecuteNonQuery();
            myconn.Close();
            message.Visible = true;
            message.Text = "编辑成功";
            classlist();
        }

    }
    protected void gv_class_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        myconn.Open();
        string sql = "delete * from BigClass  Where BigClassID=" + Convert.ToInt32(gv_class.DataKeys[e.RowIndex].Value);
        //创建数据库命令
        OleDbCommand mycmd = new OleDbCommand(sql, myconn);
        mycmd.ExecuteNonQuery();
        myconn.Close();
        message.Visible = true;
        message.Text = "删除成功";
        classlist();
    }
}
