Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to restore a sql database programmatically in c#?

how to restore a sql database programmatically in c#?

Scheduled Pinned Locked Moved C#
databasecsharptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rincy sivan
    wrote on last edited by
    #1

    hi how to restore a sql database programmatically in c#?

    N OriginalGriffO R 4 Replies Last reply
    0
    • R rincy sivan

      hi how to restore a sql database programmatically in c#?

      N Offline
      N Offline
      Nicholas Marty
      wrote on last edited by
      #2

      Use the RESTORE command and connect to the database server with an user that has access to do restores. (e.g. server admin) You can just use the normal SqlConnection and SqlCommands to do that Important Note: The path to the backup you provide are relative to the SqlServer. If it is not the same server as your application you need to copy the files to the server (or a network share?) first where the database server can restore the backup from.

      1 Reply Last reply
      0
      • R rincy sivan

        hi how to restore a sql database programmatically in c#?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Try this: Backing up an SQL Database in C#[^] - it includes code for backup and restore.

        Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • R rincy sivan

          hi how to restore a sql database programmatically in c#?

          R Offline
          R Offline
          Raj Mouli
          wrote on last edited by
          #4

          The following namespaces can be used to access SQL Server 2005 programmatically: Microsoft.SqlServer.management Microsoft.SqlServer.Management.NotificationServices Microsoft.SqlServer.Management.Smo Microsoft.SqlServer.Management.Smo.Agent Microsoft.SqlServer.Management.Smo.Broker Microsoft.SqlServer.Management.Smo.Mail Microsoft.SqlServer.Management.Smo.RegisteredServers Microsoft.SqlServer.Management.Smo.Wmi Microsoft.SqlServer.Management.Trace

          Backup bkpDBFull = new Backup();
          /* Specify whether you want to back up database or files or log */
          bkpDBFull.Action = BackupActionType.Database;
          /* Specify the name of the database to back up */
          bkpDBFull.Database = myDatabase.Name;
          /* You can take backup on several media type (disk or tape), here I am
          * using File type and storing backup on the file system */
          bkpDBFull.Devices.AddDevice(@"D:\AdventureWorksFull.bak", DeviceType.File);
          bkpDBFull.BackupSetName = "Adventureworks database Backup";
          bkpDBFull.BackupSetDescription = "Adventureworks database - Full Backup";
          /* You can specify the expiration date for your backup data
          * after that date backup data would not be relevant */
          bkpDBFull.ExpirationDate = DateTime.Today.AddDays(10);

          /* You can specify Initialize = false (default) to create a new
          * backup set which will be appended as last backup set on the media. You
          * can specify Initialize = true to make the backup as first set on the
          * medium and to overwrite any other existing backup sets if the all the
          * backup sets have expired and specified backup set name matches with
          * the name on the medium */
          bkpDBFull.Initialize = false;

          /* Wiring up events for progress monitoring */
          bkpDBFull.PercentComplete += CompletionStatusInPercent;
          bkpDBFull.Complete += Backup_Completed;

          /* SqlBackup method starts to take back up
          * You can also use SqlBackupAsync method to perform the backup
          * operation asynchronously */
          bkpDBFull.SqlBackup(myServer);

          Mouli

          1 Reply Last reply
          0
          • R rincy sivan

            hi how to restore a sql database programmatically in c#?

            R Offline
            R Offline
            Raj Mouli
            wrote on last edited by
            #5

            The following namespaces can be used to access SQL Server 2005 programmatically: Microsoft.SqlServer.management Microsoft.SqlServer.Management.NotificationServices Microsoft.SqlServer.Management.Smo Microsoft.SqlServer.Management.Smo.Agent Microsoft.SqlServer.Management.Smo.Broker Microsoft.SqlServer.Management.Smo.Mail Microsoft.SqlServer.Management.Smo.RegisteredServers Microsoft.SqlServer.Management.Smo.Wmi Microsoft.SqlServer.Management.Trace

            Backup bkpDBFull = new Backup();
            /* Specify whether you want to back up database or files or log */
            bkpDBFull.Action = BackupActionType.Database;
            /* Specify the name of the database to back up */
            bkpDBFull.Database = myDatabase.Name;
            /* You can take backup on several media type (disk or tape), here I am
            * using File type and storing backup on the file system */
            bkpDBFull.Devices.AddDevice(@"D:\AdventureWorksFull.bak", DeviceType.File);
            bkpDBFull.BackupSetName = "Adventureworks database Backup";
            bkpDBFull.BackupSetDescription = "Adventureworks database - Full Backup";
            /* You can specify the expiration date for your backup data
            * after that date backup data would not be relevant */
            bkpDBFull.ExpirationDate = DateTime.Today.AddDays(10);

            /* You can specify Initialize = false (default) to create a new
            * backup set which will be appended as last backup set on the media. You
            * can specify Initialize = true to make the backup as first set on the
            * medium and to overwrite any other existing backup sets if the all the
            * backup sets have expired and specified backup set name matches with
            * the name on the medium */
            bkpDBFull.Initialize = false;

            /* Wiring up events for progress monitoring */
            bkpDBFull.PercentComplete += CompletionStatusInPercent;
            bkpDBFull.Complete += Backup_Completed;

            /* SqlBackup method starts to take back up
            * You can also use SqlBackupAsync method to perform the backup
            * operation asynchronously */
            bkpDBFull.SqlBackup(myServer);

            for more info - http://www.mssqltips.com/sqlservertip/1849/backup-and-restore-sql-server-databases-programmatically-with-smo/[^]

            Mouli

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups