Monday, June 17, 2013

How to Backup and Restore .bak file using C# 2010

references :
http://www.geekpedia.com/tutorial180_Backup-and-restore-SQL-databases.html
http://www.codeproject.com/Articles/123441/SQL-Server-2008-Backup-and-Restore-Databases-using
http://www.c-sharpcorner.com/uploadfile/rohatash/restore-sql-server-backup-file-with-c-sharp/
http://manish4dotnet.blogspot.com/2013/03/RestoreDataBaseusingCSharp.html
......................................................................................................................................................

//Using namespaces :
// these namespace found in "c:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\"
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.SqlEnum;
using System.Collections.Specialized;


                 ServerConnection srvConn = new ServerConnection(Environment.MachineName);
                //my SQL user doesnt have sufficient permissions,
                //so i am using my windows account
                srvConn.LoginSecure = true;
                //connection.LoginSecure = false;
                //connection.Login = "testuser";
                
                //connection.Password = "testuser";
             


  public static void RestoreDB(string backUpFilePath, string databaseName)
        {
            try
            {
                Restore restore = new Restore();
                //Set type of backup to be performed to database
                restore.Database = databaseName;
                restore.Action = RestoreActionType.Database;

                // Replace the Db if already exists
                restore.ReplaceDatabase = true;

                //Set up the backup device to use filesystem.         
                restore.Devices.AddDevice(backUpFilePath, DeviceType.File);
                //set ReplaceDatabase = true to create new database
                //regardless of the existence of specified database
                restore.ReplaceDatabase = true;
                //If you have a differential or log restore to be followed,
                //you would specify NoRecovery = true
                restore.NoRecovery = false;
                Server sqlServer = new Server(srvConn);
                //SqlRestore method starts to restore database           
                restore.SqlRestore(sqlServer);
                MessageBox.Show("Restore operation succeeded");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Restore operation failed");
                MessageBox.Show(ex.Message);
            }
            Console.ReadLine();
        }
===============================
AND to call this Method :

RestoreDB(@"C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\SmallStoreme.bak", "SmallStore");

===============================
  public static void BackUpDB(string databaseName)
        {
                 // If there was a SQL connection created
                  // Create a new backup operation
                    Backup bkpDatabase = new Backup();
                    // Set the backup type to a database backup
                    bkpDatabase.Action = BackupActionType.Database;
                    // Set the database that we want to perform a backup on
                    bkpDatabase.Database =databaseName;

                    // Set the backup device to a file
                    BackupDeviceItem bkpDevice = new BackupDeviceItem(saveBackupDialog.FileName,    
                           DeviceType.File);
                    // Add the backup device to the backup
                    bkpDatabase.Devices.Add(bkpDevice);
                    // Perform the backup

                    bkpDatabase.SqlBackup(srvSql);
         }
===============================
AND to call this Method :

BackUpDB("SmallStore");

===============================