using System;
using System.IO;
using TopWinCMS.BLL;
using TopWinCMS.Common;
using TopWinCMS.BLL.PageFactory.TagModule;
namespace TopWinCMS.WebUI.Admin.DataBase
{
    public partial class SqlServer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Config.DAL.ToLower().Contains("acc"))
            {
                Response.Redirect("Access.aspx");
            }
            if (!IsPostBack)
            {
                string strAction = Request.QueryString["Action"];
                if (strAction == "Backup")
                {
                    RightsCheck.CheckWorkPower("D_1");
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    Panel3.Visible = false;
                    LB_Path.Text = "数据库管理 → SqlServer → 备份数据库";
                    this.Title = "备份数据库";
                }
                else if (strAction == "Restore")
                {
                    RightsCheck.CheckWorkPower("D_2");
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    Panel3.Visible = false;
                    LB_Path.Text = "数据库管理 → SqlServer → 还原数据库";
                    this.Title = "还原数据库";
                }
                else if (strAction == "Compact")
                {
                    RightsCheck.CheckWorkPower("D_1");
                    Panel1.Visible = false;
                    Panel2.Visible = false;
                    Panel3.Visible = true;
                    LB_Path.Text = "数据库管理 → SqlServer → 压缩数据库";
                    this.Title = "压缩数据库";
                }
            }
        }

        protected void BT_Backup_Click(object sender, EventArgs e)
        {
            string strDataBaseName = TagEngine.GetValue(Config.ConnectionSqlServer, "Database");

            string Dir = Path.GetDirectoryName(TB_Backup.Text.Trim());
            if (Directory.Exists(Dir))
            {
                Directory.CreateDirectory(Dir);
            }

            string strSql = string.Format("backup database {0} to disk='{1}'", strDataBaseName, TB_Backup.Text.Trim());

            try
            {
                using (SQLBase MyBase = new SQLBase())
                {
                    MyBase.ExecuteNonQuery(strSql);
                }
                MsgBox.Alert("备份完成");
            }
            catch (Exception error)
            {
                MsgBox.Alert(error.Message);
            }

        }

        protected void BT_Restore_Click(object sender, EventArgs e)
        {
            string strDataBaseName = TagEngine.GetValue(Config.ConnectionSqlServer, "Database");

            if (File.Exists(TB_Restore.Text.Trim()) == false)
            {
                MsgBox.Alert("文件不存在！");
                return;
            }

            string SqlStr2 = string.Format("use master restore database {0} from disk='{1}'", strDataBaseName, TB_Restore.Text.Trim());

            try
            {
                using (SQLBase MyBase = new SQLBase())
                {
                    MyBase.ExecuteNonQuery(SqlStr2);
                }
                MsgBox.Alert("备份完成");
            }
            catch (Exception error)
            {
                MsgBox.Alert(error.Message);
            }
        }

        protected void BT_Compact_Click(object sender, EventArgs e)
        {

            string strDataBaseName = TagEngine.GetValue(Config.ConnectionSqlServer, "Database");

            string SqlStr2 = string.Format("DBCC SHRINKDATABASE(N'{0}' )", strDataBaseName);

            try
            {
                using (SQLBase MyBase = new SQLBase())
                {
                    MyBase.ExecuteNonQuery(SqlStr2);
                }
                MsgBox.Alert("压缩完成");
            }
            catch (Exception error)
            {
                MsgBox.Alert(error.Message);
            }
        }
    }
}