title: Using Smo.Backup to backup SQL Server database to string
body: <p>I'm trying to make a little app that would help me making my server backup. That app would run on my home PC so the main goal is to be able to connect to the external server, backup the selected database, dump the backup content to a string or something so I could write it on my PC disk and not the server's disk.</p>
  
  <p>I did that which works to write on the server's disk, but I'd like to be able to write on my PC's disk the backup's result.</p>
  
  <pre><code>private bool BackupDatabase()
  {
      try
      {
          // Filename
          string sFileName = string.Format("{0}\\{1}.bak", _sWhereToBackup, DatabaseName);
  
          // Connection
          string sConnectionString = String.Format(
              "Data Source=tcp:{0};Initial Catalog={1};User ID={2};Pwd={3};",
              DatabaseHost, DatabaseName, DatabaseUsername, DatabasePassword);
  
          SqlConnection oSqlConnection = new SqlConnection(sConnectionString);
          Server oServer = new Server(new ServerConnection(oSqlConnection));
  
          // Backup
          Backup backup = new Backup();
          backup.Action = BackupActionType.Database;
          backup.Database = DatabaseName;
          backup.Incremental = false;
          backup.Initialize = true;
          backup.LogTruncation = BackupTruncateLogType.Truncate;
  
          // Backup Device
          BackupDeviceItem backupItemDevice = new BackupDeviceItem(sFileName, DeviceType.File);
          backup.Devices.Add(backupItemDevice);
  
          // Start Backup
          backup.SqlBackup(oServer);
      }
      catch (Exception ex)
      {
          throw ex;
      }
  
      return false;
  }
  </code></pre>
  
  <p>Thanks so much!</p>