using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;



namespace GymSystem
{
    public partial class FrmRestore : Form
    {
        public FrmRestore()
        {
            InitializeComponent();
        }

        private void PicBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "BackUp Files (*.bak)|*.bak";
            ofd.Title = "فایل پشتیبان خود را انتخاب کنید";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                TxtAdress.Text = ofd.FileName;
            }



        }

        private void BtnRestore_Click(object sender, EventArgs e)
        {
            


            FrmMessage fm = new FrmMessage();

            if (TxtAdress.Text == "")
            {
                fm.Text = "بازیابی پشتیبان";
                fm.LblText.Text = "فایل پشتیبان جهت بازیابی را انتخاب کنید";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;

                fm.ShowDialog();
                return;
            }



            try
            {

                
                StreamReader sr = new StreamReader(Application.StartupPath + "\\ServerName.bas");
                string StrServerName = sr.ReadLine();
                sr.Close();
                SqlConnection con = new SqlConnection("Server = " + StrServerName + " ; DataBase = DbGym ; Integrated Security = True");
               
                SqlConnection.ClearAllPools();

                SqlCommand cmd = new SqlCommand("USE [master]; RESTORE DATABASE [DbGym]  FROM  DISK = N'" + TxtAdress.Text + "' WITH NOUNLOAD,  REPLACE,  STATS = 10", con);


                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


                fm.Text = "بازیابی پشتیبان";
                fm.LblText.Text = "عمل بازیابی با موفقیت انجام شد";
                fm.PicOk.Visible = true;
                fm.PicKhabar.Visible = true;
                fm.ShowDialog();


                Application.Restart();
            }
            catch
            {
                fm.Text = "بازیابی پشتیبان";
                fm.LblText.Text = "خطا در بازیابی پشتیبان";
                fm.PicOk.Visible = true;
                fm.PicHoshdar.Visible = true;
                fm.ShowDialog();

            }
           




        }

        private void FrmRestore_Load(object sender, EventArgs e)
        {

        }
    }
}
