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;
using worm;


public partial class wormadmin_db_manager : System.Web.UI.Page
{
    sqloper sqloper = new sqloper();
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["superad"] == null || Session["superpw"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            Page.Theme = "SkinFile";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            SqlConnection one = conn.sqlconn();
            gvbind("select * from db_backup order by id desc", one, myGrid, AspNetPager1.PageSize);
        }

    }

    #region 数据库备份列表表操作-----------------------------------------------------------------
    //数据绑定GridView
    //sql:数据库查询语句,conn:数据库连接,gvid:gridview控件的id,pssize:一页中数据的多少
    protected void gvbind(string sql, SqlConnection conn, GridView gvid, int pssize)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        PagedDataSource ps = new PagedDataSource();
        try
        {
            conn.Open();
            adapter.Fill(ds, "order");
            ps.DataSource = ds.Tables["order"].DefaultView;              //ps分页数据源设置
            ps.AllowPaging = true;                                       //允许分页
            ps.PageSize = pssize;                                       //页面数据大小
            ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;    //当前页面指针
            AspNetPager1.RecordCount = ds.Tables["order"].Rows.Count;  //记录总行数赋值给控件AspNetPager1

            gvid.DataSource = ps;
            gvid.DataBind();
        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }
        finally
        {
            ds.Dispose();
            adapter.Dispose();
            conn.Close();
        }
    }



    //翻页数据绑定事件
    protected void pagego(object sender, EventArgs e)
    {
        SqlConnection one = conn.sqlconn();
        gvbind("select * from db_backup order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //选中删除按钮事件
    protected void btdel_Click(object sender, EventArgs e)
    {
        SqlConnection one = conn.sqlconn();
        int i;
        string sql;
        string myKeyid;
        CheckBox mycheck = new CheckBox();
        string delfile;
        myupimg oper = new myupimg();         //文件操作类
        for (i = 0; i < myGrid.Rows.Count; i++)
        {
            mycheck = (CheckBox)myGrid.Rows[i].FindControl("del");
            delfile = ((Label)myGrid.Rows[i].FindControl("bkname")).Text;
            if (mycheck.Checked == true)
            {
                myKeyid = myGrid.DataKeys[i].Value.ToString();
                //删除数据库里面的数据库备份记录
                sql = "DELETE FROM db_backup where id=" + myKeyid;
                sqloper.dodata(sql, one);
                //物理删除数据备份
                oper.DeleteFile(@"backup\"+delfile+".bat");
                ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('成功删除备份数据库!');</script>");
            }
        }
        gvbind("select * from db_backup order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    protected void delall_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked == true)
            {
                ch.Checked = true;
            }
            else
            {
                ch.Checked = false;
            }
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (ch.Checked == false)
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = true;
                ch.Checked = true;
            }
            else
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = false;
                ch.Checked = false;
            }
        }
    }
    #endregion----------------------------------------------------------------------------------


    //数据库备份按钮单击事件
    protected void btbk_Click1(object sender, EventArgs e)
    {
        SqlConnection one = conn.sqlconn();
        myupimg oper = new myupimg();         //文件操作类
        DbOper dbdo = new DbOper();          //数据库备份类
        string bkname=btbkname.Text;
        string bkpath = Server.MapPath(@"backup\" + bkname + ".bat");
        oper.CreateFolder(@"backup\");
        string bktime=DateTime.Now.ToString("d");
        string rstime="";

        //数据库备份并写入数据库
        dbdo.DbBackup("127.0.0.1", "sa", "sa", "wormbook", bkpath, bkname, "数据备份");
        string sql = "Declare @dbid Integer;" + Convert.ToChar(13);
        sql = sql + "select @dbid=(case when max(id) is null then 1 else max(id)+1 end) from db_backup;" + Convert.ToChar(13);
        sql = sql + "insert into db_backup values(@dbid,'" + bkname + "','" + bkpath + "','" + bktime + "','" + rstime + "');";
        sqloper.dodata(sql, one);

        //数据重新绑定
        gvbind("select * from db_backup order by id desc", one, myGrid, AspNetPager1.PageSize);
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('备份数据库成功!');</script>");
    }

    //数据库还原按钮单击事件
    protected void btres_Click(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "res")
        {
            SqlConnection one = conn.sqlconn();
            myupimg oper = new myupimg();         //文件操作类
            DbOper dbdo = new DbOper();          //数据库备份类
            int resindex = Convert.ToInt16(e.CommandArgument);    //获取选取行
            string respath = ((Label)myGrid.Rows[resindex].FindControl("bkpath")).Text;
            string resname = "wormbook";
            string restime = DateTime.Now.ToString("d");

            //数据库备份并写入数据库
            dbdo.DbRestore("127.0.0.1", "sa", "sa", resname, respath);
            string sql = "update db_backup set rstime='" + restime + "' where id=" + myGrid.DataKeys[resindex].Value;
            sqloper.dodata(sql, one);

            gvbind("select * from db_backup order by id desc", one, myGrid, AspNetPager1.PageSize);
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('还原数据库成功!');</script>");
        }
    }
}
