Example #1
0
        /// <summary>
        /// Creates a backup of the specified database using the specified <paramref name="device"/>
        /// and <paramref name="destination"/>.
        /// </summary>
        /// <param name="device">
        /// A <see cref="DbBackupDevice"/> defining the type of output device.
        /// </param>
        /// <param name="destination">
        /// Device path.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="destination"/> is a null
        /// reference (Nothing in Visual Basic)
        /// </exception>
        public override void BackupDatabase(DbBackupDevice device, string destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            string query = null;

            if (device == DbBackupDevice.Dump)
            {
                query = String.Format("BACKUP DATABASE [{0}] TO {1}",
                                      this.DatabaseName,
                                      destination
                                      );
            }
            else
            {
                query = String.Format("BACKUP DATABASE [{0}] TO {1} = '{2}'",
                                      this.DatabaseName,
                                      device.ToString().ToUpper(),
                                      destination
                                      );
            }
            this.EnsureFileDestination(destination);
            this.ExecuteNonQuery(this.ConnectionString, query);
        }
Example #2
0
        /// <summary>
        /// Restores a backup of the specified database using the specified <paramref name="device"/>
        /// and <paramref name="destination"/>.
        /// </summary>
        /// <param name="device">
        /// A <see cref="DbBackupDevice"/> defining the type of output device.
        /// </param>
        /// <param name="destination">
        /// Device path.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="destination"/> is a null
        /// reference (Nothing in Visual Basic)
        /// </exception>
        /// <remarks>
        /// <para>
        /// If you plan to override an existing database, you must first drop this database.
        /// This method takes a conservative behavior and will not override an existing database.
        /// </para>
        /// <para>
        /// Priorly to restore the database, the method kills all the processes associeted
        /// to the database.
        /// </para>
        /// </remarks>
        public override void RestoreDatabase(DbBackupDevice device, string destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            // kill processes on db
            this.killOpenDBProcesses(this.ConnectionString, this.DatabaseName);

            string query = null;

            if (device == DbBackupDevice.Dump)
            {
                query = String.Format("RESTORE DATABASE [{0}] FROM {1}",
                                      this.DatabaseName,
                                      destination
                                      );
            }
            else
            {
                query = String.Format("RESTORE DATABASE [{0}] FROM {1} = '{2}'",
                                      this.DatabaseName,
                                      device.ToString().ToUpper(),
                                      destination
                                      );
            }

            this.ExecuteNonQuery(this.ConnectionString, query);
        }