Click here to Skip to main content
15,357,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends,
Here am sharing the backup and restore code for SQL Server 2008.In that I can able to make backup but I cant able to identify whether restore operation is handling or not,Please share the comments about the code.
And the code for backup:
C#
private void btnBackup_Click(object sender, EventArgs e)
        {
             if (cmbDatabase.Text.CompareTo("") == 0)
            {
                MessageBox.Show("Please Select Database");
                return;
            }
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();
                string sql = "BACKUP DATABASE " + cmbDatabase.Text + " TO DISK = '" + txtDBBackup.Text + "\\" + cmbDatabase.Text + "-" + DateTime.Now.Ticks.ToString() + ".bak'";
                cmd = new SqlCommand(sql, con);
                cmd.ExecuteNonQuery();
                txtDBBackup.Text = "";
                MessageBox.Show("Successfully Backed Up.");
                txtDBBackup.Text = string.Empty;
                con.Close();
                con.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

And the code for restore is:
C#
private void btnRestore_Click(object sender, EventArgs e)
      {
          try
          {
              if (cmbDatabase.Text.CompareTo("") == 0)
              {
                  MessageBox.Show("Please select a Database.");
                  return;
              }
              con = new SqlConnection(connectionString);
              con.Open();
              sql = "Alter Database " + cmbDatabase.Text + " Set SINGLE_USER WITH ROLLBACK IMMEDIATE;";
              sql += "Restore Database " + cmbDatabase.Text + " FROM Disk = '" + txtDBRestore.Text + "' WITH REPLACE;";
              cmd = new SqlCommand(sql, con);
              cmd.ExecuteNonQuery();
              con.Close();
              con.Dispose();
              MessageBox.Show("Successfully Restore Database");
              txtDBRestore.Text = string.Empty;
              cmbDatabase.Text = string.Empty;
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
Posted
Comments
ZurdoDev 4-Nov-13 8:55am
   
What happens when you try to restore?
Sabari Karthik 6-Nov-13 5:12am
   
it shows only the message but not restored.please share how i can restore the database.
ZurdoDev 6-Nov-13 7:22am
   
What message? Just debug it and tell us what happens.
Bheeshm 5-Nov-13 8:13am
   
Pls Try Executing the SQL on some DataBase to Test ! Though You can BackUp and Restore in the Same DataBase Right ?

(1) View the Tutorial

http://www.youtube.com/watch?v=K2C_ia0KGCc&feature=youtu.be

(2) Try out the code
use my above mentioned free control and njoy Database operations with less code
C#
dcon.ExecNonQuery("delete from folderPath");
dcon.ExecNonQuery("BACKUP DATABASE Timber_Demo TO  DISK = N'" + txtFName.Text + "'");
MessageBox.Show("Backup Done Successfully");

//Restore Operation
C#
txtFName.Text = openFileDialog1.FileName;
                   dcc = new DBConClass();
                   // dcc.setSQLConnection(@".\sqlexpress", "master", "sa", "sql");
                   dcc.setSQLConnection(@".", "master", "sa", "sql");
                   string alt = "ALTER DATABASE Timber_Demo SET OFFLINE WITH ROLLBACK IMMEDIATE";
                   dcc.ExecNonQuery(alt);

                   dcc = new DBConClass();
                   // dcc.setSQLConnection(@".\sqlexpress", "master", "sa", "sql");
                   dcc.setSQLConnection(@".", "master", "sa", "sql");
                   string restore = "restore database Timber_Demo from DISK =N'" + txtFName.Text + "'WITH FILE=1,NOUNLOAD,REPLACE,Stats=10";
                   int res = dcc.ExecNonQuery(restore);


                   dcc = new DBConClass();
                   //  dcc.setSQLConnection(@".\sqlexpress", "master", "sa", "sql");
                   dcc.setSQLConnection(@".", "master", "sa", "sql");
                   alt = "ALTER DATABASE Timber_Demo SET ONLINE";
                   dcc.ExecNonQuery(alt);

                   MessageBox.Show("Data Restored successfully");

                   txtFName.Text = "";
                   // dcc.setSQLConnection(@".\sqlexpress", "DBStore", "sa", "sql");
                   dcc.setSQLConnection(@".", "Timber_Demo", "sa", "sql");
   
v2
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900