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;
using System.IO;
using DevComponents.DotNetBar;

namespace amlak_keshavarzi
{
    public partial class restore : DevComponents.DotNetBar.Metro.MetroForm
    {
        static SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\keshavarzi.mdf;Integrated Security=True");
        OpenFileDialog dlg = new OpenFileDialog();
        public restore()
        {
            InitializeComponent();
        }

        private void btn_restore_Click(object sender, EventArgs e)
        {
            if (txt_address.Text == "")
            {
                MessageBox.Show("لطفاً آدرس برای فایل پشتبلان انتخاب کنید", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            else
            {
                string rs;
                rs = @"use master ALTER DATABASE [" + Application.StartupPath + "\\keshavarzi.mdf]  SET SINGLE_USER with ROLLBACK IMMEDIATE " +
                        "RESTORE DATABASE [" + Application.StartupPath + "\\keshavarzi.mdf] FROM DISK='" + dlg.FileName + "' with REPLACE " +
                        "ALTER DATABASE [" + Application.StartupPath + "\\keshavarzi.mdf] SET MULTI_USER ";
                con.Open();
                SqlCommand com = new SqlCommand(rs,con);
                com.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("بازیبابی پشتیبان با موفقیت انجام شد", "پیغام", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Application.Restart();
 
            }
        }

        private void btn_browes_Click(object sender, EventArgs e)
        {
            
            dlg.Filter = "BackUp Files(*.BAK)|*.BAK";
            dlg.FilterIndex = 1;
            dlg.Title = "بازیابی پشتیبان";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txt_address.Text = dlg.FileName.ToString();
            }
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            main f = new main();
            f.Show();
            this.Hide();
        }

      
    }
}
