using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using TinyHeels.App_Code;

namespace TinyHeels
{
    public partial class Backup : Form
    {
        public Backup()
        {
            InitializeComponent();
        }



        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    txtBackup.Text = dlg.SelectedPath.ToString();
                }
            }
            catch (Exception ex)
            {
                ErrorBox er = new ErrorBox("You should select the path");
                er.ShowDialog();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
        }

        private void backup(object sender, EventArgs e)
        {
            string str = @"data source=(local)\SQLEXPRESS; database=TinyHeels; integrated security = SSPI";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            try
            {
                string cmd = "BACKUP DATABASE [TinyHeels] TO DISK='" + txtBackup.Text + "\\Classic" + "-" + DateTime.Now.ToString("yyyy-MM-dd-mm-ss") + ".bak'";
                using (SqlCommand command = new SqlCommand(cmd, con))
                {
                    command.ExecuteNonQuery();
                    con.Close();
                    ErrorBox d = new ErrorBox("Done Backup");
                    d.ShowDialog();
                    txtBackup.Text = "";
                }
            }
            catch (Exception ex)
            {
                con.Close();
                ErrorBox er = new ErrorBox("You should select a valid path");
                er.ShowDialog();
            }
        }

        private void browsrestore(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "SQL Server Database  Backup File | *.bak";
                dlg.Title = "Database Restore";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    txtRestore.Text = dlg.FileName;

                }
            }
            catch (Exception ex)
            {
                ErrorBox er = new ErrorBox("You should select the path");
                er.ShowDialog();
            }
        }

        private void rest(object sender, EventArgs e)
        {
            string str = @"data source=(local)\SQLEXPRESS; database=TinyHeels; integrated security = SSPI";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            try
            {
                string st1 = string.Format("ALTER DATABASE [TinyHeels] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
                SqlCommand s1 = new SqlCommand(st1, con);
                s1.ExecuteNonQuery();

                string st2 = string.Format("USE MASTER RESTORE DATABASE [TinyHeels] FROM DISK='" + txtRestore.Text + "' WITH REPLACE");
                SqlCommand s2 = new SqlCommand(st2, con);
                s2.ExecuteNonQuery();

                string st3 = string.Format("ALTER DATABASE [TinyHeels] SET MULTI_USER");
                SqlCommand s3 = new SqlCommand(st3, con);
                s3.ExecuteNonQuery();

                ErrorBox d = new ErrorBox("Done Backup");
                d.ShowDialog();
                txtRestore.Text = "";
            }
            catch (Exception ex)
            {
                con.Close();
                ErrorBox er = new ErrorBox("You should select a valid path");
                er.ShowDialog();
                //MessageBox.Show(ex.ToString());
            }
        }
    }
}
