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




namespace HotelProject
{
    public partial class FrmRestore : Form
    {
        SqlConnection con;
        SqlCommand cmd;



        public FrmRestore()
        {
            InitializeComponent();
        }

        private void BtnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "(*.bak)|*.bak";
            openFileDialog1.Title = "فایل پشتیبان خود را انتخاب کنید";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                TxtPatch.Text = openFileDialog1.FileName;
            }
        }

        private void BtnRestore_Click(object sender, EventArgs e)
        {
            if (TxtPatch.Text == "")
            {
                errorProvider1.SetError(TxtPatch, "فایل پشتیبان خود را انتخاب کنید");
                return;
            }
            errorProvider1.Clear();

            BtnRestore.Enabled = false;
            BtnBrowse.Enabled = false;

            try
            {






               con = new SqlConnection("Server = . ; DataBase = master ; Integrated Security = True");

              // cmd = new SqlCommand("DoRestore", con);
             // cmd.CommandType = CommandType.StoredProcedure;
             //  cmd.Parameters.AddWithValue("@pathfile", TxtPatch.Text);
             //  con.Open();
             //  cmd.ExecuteNonQuery();
             //  con.Close();






               cmd = new SqlCommand();
               SqlConnection.ClearAllPools();
               string query = "USE [master]; RESTORE DATABASE [DbHotel]  FROM  DISK = N'" + TxtPatch.Text + "' WITH NOUNLOAD,  REPLACE,  STATS = 10";
               cmd.Connection = con;
               cmd.CommandTimeout = 100;
               con.Open();
               cmd.CommandText = query;
               cmd.ExecuteNonQuery();
               con.Close();


              FrmMessage Fm = new FrmMessage();
                Fm.LblText.Text = "بازیابی با موفقیت انجام شد.هم اکنون برنامه مجددا راه اندازی میشود";
                Fm.PicClose.Visible = true;
                Fm.PicNO.Visible = false;
                Fm.PicYes.Visible = false;
                Fm.ShowDialog();


                BtnRestore.Enabled = true;
                BtnBrowse.Enabled = true;


                Application.Restart();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                FrmMessage Fm = new FrmMessage();
                Fm.LblText.Text = "بازیابی با مشکل مواجه شد";
                Fm.PicClose.Visible = true;
                Fm.PicNO.Visible = false;
                Fm.PicYes.Visible = false;
                Fm.ShowDialog();


                BtnRestore.Enabled = true;
                BtnBrowse.Enabled = true;
            }
            
        }

        private void FrmRestore_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("Server = . ; DataBase = DbHotel ; Integrated Security = True");
        }

        private void FrmRestore_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (BtnRestore.Enabled == false)
            {
                e.Cancel = true;
            }
        }
    }
}
