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. SQL Backing up with SMO & C# ?

SQL Backing up with SMO & C# ?

Scheduled Pinned Locked Moved C#
databasehelpquestioncsharpcom
4 Posts 3 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.
  • M Offline
    M Offline
    Mohammad Dayyan
    wrote on last edited by
    #1

    I'm using the following code to back up a SQL Database :

    void BackupDatabase(string sConnect, string dbName, string backUpPath)
    {
    using (SqlConnection cnn = new SqlConnection(sConnect))
    {
    cnn.Open();
    dbName = cnn.Database.ToString();

        ServerConnection sc = new ServerConnection(cnn);
        Server sv = new Server(sc);
    
        // Create backup device item for the backup
        BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);
    
        // Create the backup informaton
        Microsoft.SqlServer.Management.Smo.Backup bk = new Backup();
        bk.PercentComplete += new PercentCompleteEventHandler(percentComplete);
        bk.Devices.Add(bdi);
        bk.Action = BackupActionType.Database;
        bk.PercentCompleteNotification = 1;
        bk.BackupSetDescription = dbName;
        bk.BackupSetName = dbName;
        bk.Database = dbName;
        //bk.ExpirationDate = DateTime.Now.AddDays(30);
        bk.LogTruncation = BackupTruncateLogType.Truncate;
        bk.FormatMedia = false;
        bk.Initialize = true;
        bk.Checksum = true;
        bk.ContinueAfterError = true;
        bk.Incremental = false;
    
        // Run the backup
        bk.SqlBackup(sv);
    }
    

    }

    In my system (Win7 x64) it works fine but in destination system (WinXP SP3 x86) I receive the below error : Exception[^] How can I fix it ? Thanks. BTW: I installed SQL Express 2008 R2 in destination system.

    R L 2 Replies Last reply
    0
    • M Mohammad Dayyan

      I'm using the following code to back up a SQL Database :

      void BackupDatabase(string sConnect, string dbName, string backUpPath)
      {
      using (SqlConnection cnn = new SqlConnection(sConnect))
      {
      cnn.Open();
      dbName = cnn.Database.ToString();

          ServerConnection sc = new ServerConnection(cnn);
          Server sv = new Server(sc);
      
          // Create backup device item for the backup
          BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);
      
          // Create the backup informaton
          Microsoft.SqlServer.Management.Smo.Backup bk = new Backup();
          bk.PercentComplete += new PercentCompleteEventHandler(percentComplete);
          bk.Devices.Add(bdi);
          bk.Action = BackupActionType.Database;
          bk.PercentCompleteNotification = 1;
          bk.BackupSetDescription = dbName;
          bk.BackupSetName = dbName;
          bk.Database = dbName;
          //bk.ExpirationDate = DateTime.Now.AddDays(30);
          bk.LogTruncation = BackupTruncateLogType.Truncate;
          bk.FormatMedia = false;
          bk.Initialize = true;
          bk.Checksum = true;
          bk.ContinueAfterError = true;
          bk.Incremental = false;
      
          // Run the backup
          bk.SqlBackup(sv);
      }
      

      }

      In my system (Win7 x64) it works fine but in destination system (WinXP SP3 x86) I receive the below error : Exception[^] How can I fix it ? Thanks. BTW: I installed SQL Express 2008 R2 in destination system.

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      Is the version of .NET on the XP system the same as that on your Win7 system?  If not, you may want to try to build against a lower version if possible. /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      M 1 Reply Last reply
      0
      • M Mohammad Dayyan

        I'm using the following code to back up a SQL Database :

        void BackupDatabase(string sConnect, string dbName, string backUpPath)
        {
        using (SqlConnection cnn = new SqlConnection(sConnect))
        {
        cnn.Open();
        dbName = cnn.Database.ToString();

            ServerConnection sc = new ServerConnection(cnn);
            Server sv = new Server(sc);
        
            // Create backup device item for the backup
            BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);
        
            // Create the backup informaton
            Microsoft.SqlServer.Management.Smo.Backup bk = new Backup();
            bk.PercentComplete += new PercentCompleteEventHandler(percentComplete);
            bk.Devices.Add(bdi);
            bk.Action = BackupActionType.Database;
            bk.PercentCompleteNotification = 1;
            bk.BackupSetDescription = dbName;
            bk.BackupSetName = dbName;
            bk.Database = dbName;
            //bk.ExpirationDate = DateTime.Now.AddDays(30);
            bk.LogTruncation = BackupTruncateLogType.Truncate;
            bk.FormatMedia = false;
            bk.Initialize = true;
            bk.Checksum = true;
            bk.ContinueAfterError = true;
            bk.Incremental = false;
        
            // Run the backup
            bk.SqlBackup(sv);
        }
        

        }

        In my system (Win7 x64) it works fine but in destination system (WinXP SP3 x86) I receive the below error : Exception[^] How can I fix it ? Thanks. BTW: I installed SQL Express 2008 R2 in destination system.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        A simple Google action yields this[^]. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        1 Reply Last reply
        0
        • R Ravi Bhavnani

          Is the version of .NET on the XP system the same as that on your Win7 system?  If not, you may want to try to build against a lower version if possible. /ravi

          My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

          M Offline
          M Offline
          Mohammad Dayyan
          wrote on last edited by
          #4

          Ravi Bhavnani wrote:

          Is the version of .NET on the XP system the same as that on your Win7 system?

          The version that I've created the code based on was 9 but the version of destination system is 10, I created the project with 10 version but I receive below Exception :

          Restore failed for Server '\\\\.\\pipe\\3F103E6E-3FD4-47\\tsql\\query'.

          :(

          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