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


namespace MainTemplate
{
    public partial class frmBackupDB : Form
    {
        SqlConnection con;
        SqlConnection con1;
        SqlCommand cmd ;
        SqlDataReader dread;


        public frmBackupDB()
        {
            InitializeComponent();
        }

        public void server(string str)
        {
            con = new SqlConnection("Data Source=" + str + ";Database=Master;integrated security=SSPI;");
            con.Open();
            cmd = new SqlCommand("select *  from sysservers  where srvproduct='SQL Server'", con);
            dread = cmd.ExecuteReader();
            while (dread.Read())
            {
                cmbserver.Items.Add(dread);
            }
            dread.Close();
        }
        public void connection()
        {
            con = new SqlConnection(Properties.Settings.Default.HotelResJadConnectionString.ToString());
            con.Open();
            cmbdatabase.Items.Clear();
            cmd = new SqlCommand("select * from sysdatabases", con);
            dread = cmd.ExecuteReader();
            while (dread.Read())
            {
                cmbdatabase.Items.Add(dread);
            }
            dread.Close();
        }


        public void query(string que)
        {
             cmd = new SqlCommand(que, con);
            cmd.ExecuteNonQuery();
        }


        public void blank(string str)
        {
            try
            {
                if (str == "backup")
                {
                    Timer1.Enabled = true;
                    ProgressBar1.Visible = true;
                    string s = null;
                    s = Txt_DbBackupPath.Text.Trim();
                    query("backup database  HotelResJad to disk='" + s + "'");
                }
                else if (str == "restore")
                {
                    OpenFileDialog1.ShowDialog();
                    Timer1.Enabled = true;
                    ProgressBar1.Visible = true;
                    query("RESTORE DATABASE HotelResJad FROM disk='" + Txt_DbBackupPath.Text.Trim() + "'");
                }
            }
            catch(Exception EE)
            {
                MessageBox.Show(EE.Message.ToString());
            }
        }


        private void Btn_CreateBackup_Click(object sender, EventArgs e)
        {
            Class_Db_Connection db = new Class_Db_Connection();

            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                Txt_DbBackupPath.Text = folderBrowserDialog1.SelectedPath;
                if (Txt_DbBackupPath.Text.Substring(Txt_DbBackupPath.Text.Length - 1, 1).Trim() != "\\")
                {
                    Txt_DbBackupPath.Text += "\\" + "DbBack_" + db.dtShamsi().ToString().Replace("/" , "").ToString() + DateTime.Now.Ticks.ToString() + ".bak";
                }
                else
                {
                    Txt_DbBackupPath.Text += "DbBack_" + db.dtShamsi().ToString().Replace("/", "").ToString() + DateTime.Now.Ticks.ToString() + ".bak";
                }

            }
        }
        
        private void frmBackupDB_Load(object sender, EventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " باز شد ");
            server("(local)");
        }

        private void frmBackupDB_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " بسته شد ");
        }

        private void Btn_Createttt_Click(object sender, EventArgs e)
        {
            if (Txt_DbBackupPath.Text == "")
            {
                MessageBox.Show("لطفا مسیر را انتخاب نمایید . . .");
                return;
            }
            Class_Db_Connection db = new Class_Db_Connection();

            blank("backup");
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (ProgressBar1.Value == 100)
            {
                Timer1.Enabled = false;
                ProgressBar1.Visible = false;
                MessageBox.Show("Successfully Done");
            }
            else
            {
                ProgressBar1.Value = ProgressBar1.Value + 5;
            }

        }

        private void cmbbackup_Click(object sender, EventArgs e)
        {
            blank("backup");
        }

        private void cmdrestore_Click(object sender, EventArgs e)
        {
            blank("restore");
        }
    }
}
