Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

pulic partial class form1

SqlConnection con = new SqlConnection("server= DESKTOP-BVOL8AD\\


NURANI;Database=health;integrated security=True");

first browse
FolderBrowserDialog dlg = new FolderBrowserDialog();
if(dlg.ShowDialog()==DialogResult.OK)
{
textbox1.Text = dlg.SelectedPath;
btnupdate.Enabled = true;
}

backup btn code


string database = con.Database.ToString();
if(textbox1.Text==string.Empty)
{
MessageBox.Show("please enter backup file location", "information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string cmd = "BACKUP DATABASE [" + database + "]TO DISK='" +
textbox1.Text + "//" + "database" + "-" + DateTime.Now.ToString("yyyy-mm-dd--hh-mm-
ss") + ".bak'";
con.Open();
SqlCommand command = new SqlCommand(cmd, con);
command.ExecuteNonQuery();
MessageBox.Show("your database has been backed up", "information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
btnupdate.Enabled = false;
}

restore browse code


OpenFileDialog dlg = new OpenFileDialog ();

dlg.Title="database restore";
if (dlg.ShowDialog() == DialogResult.OK)
{
textbox2.Text = dlg.FileName;
guna2Button1.Enabled = true;
}

restore btn code


string database = con.Database.ToString();
con.Open();
try
{
string str1 = string.Format("ALTER database [" + database + "]SET
SINGLE_USER WITH ROLLBACK IMMEDIATE");
SqlCommand cmd1 = new SqlCommand(str1, con);
cmd1.ExecuteNonQuery();

string str2 = "USE MASTER RESTORE DATABASE[" + database + "]FROM


DISK='" + textbox2.Text + "'WITH REPLACE;";
SqlCommand cmd2 = new SqlCommand(str2, con);
cmd2.ExecuteNonQuery();

string str3 = string.Format("ALTER database [" + database + "]SET


MULTI_USER");
SqlCommand cmd3 = new SqlCommand(str3, con);
cmd3.ExecuteNonQuery();
MessageBox.Show("your database has been restored", "information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Open();
guna2Button1.Enabled = false;

}
catch
{

You might also like