asp. net example of a method for backing up and restoring a database

  • 2020-11-26 18:45:31
  • OfStack


/**********************************************************************************
 *
 *  Functional specifications : Backup and Restore SQL Server The database 
 *  The author :  Liu feats ;
 *  version :V0.1(C#2.0); time :2007-1-1
 *  When using SQL Server when , Please refer to  COM In the component ,SQLDMO.dll component 
 *  When using Access In the , Browse to add references to the following two dll
 *           reference C:\Program Files\Common Files\System\ado\msadox.dll, the DLL contains ADOX The namespace 
 *           reference C:\Program Files\Common Files\System\ado\msjro.dll, the DLL contains JRO The namespace 
 * *******************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using ADOX;// This namespace contains creation ACCESS The class of ( methods )-- The solution  ==>  reference  ==>  Add reference  ==>  Visit to find .dll
using JRO;// This namespace contains compression ACCESS The class of ( methods )
namespace EC
{
    /// <summary>
    ///  Database recovery and backup 
    /// </summary>
    public class SqlBackObject
    {
        public SqlBackObject()
        {
            //
            // TODO:  Add the constructor logic here 
            //
        }
        #region SQL Database backup 
       /// <summary>
        /// SQL Database backup 
       /// </summary>
       /// <param name="ServerIP">SQL The server IP or (Localhost)</param>
       /// <param name="LoginName"> Database login name </param>
       /// <param name="LoginPass"> Database login password </param>
       /// <param name="DBName"> The database name </param>
       /// <param name="BackPath"> The path to which to back up </param>
        public static void SQLBACK(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
        {
            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(ServerIP, LoginName, LoginPass);
                oBackup.Database = DBName;
                oBackup.Files = BackPath;
                oBackup.BackupSetName = DBName;
                oBackup.BackupSetDescription = " Database backup ";
                oBackup.Initialize = true;
                oBackup.SQLBackup(oSQLServer);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                oSQLServer.DisConnect();
            }
        }
        #endregion
        #region SQL Restore database 
        /// <summary>
        /// SQL Restore database 
        /// </summary>
        /// <param name="ServerIP">SQL The server IP or (Localhost)</param>
        /// <param name="LoginName"> Database login name </param>
        /// <param name="LoginPass"> Database login password </param>
        /// <param name="DBName"> The name of the database to restore </param>
        /// <param name="BackPath"> The path to the database backup </param>
        public static void SQLDbRestore(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
        {

            SQLDMO.Restore orestore = new SQLDMO.RestoreClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(ServerIP, LoginName, LoginPass);
                orestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
                orestore.Database = DBName;
                orestore.Files = BackPath;
                orestore.FileNumber = 1;
                orestore.ReplaceDatabase = true;
                orestore.SQLRestore(oSQLServer);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                oSQLServer.DisConnect();
            }
        }

        #endregion
        #region  Creates according to the specified file name Access The database 
        /// <summary>
        ///  Creates data based on the specified file name 
        /// </summary>
        /// <param name="DBPath"> An absolute path + The file name </param>
        public static void CreateAccess(string DBPath)
        {
            if (File.Exists(DBPath))// Check that the database already exists 
            {
                throw new Exception(" The target database already exists , Unable to create the ");
            }          
            DBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPath;
            // create 1 a CatalogClass Object instance 
            ADOX.CatalogClass cat = new ADOX.CatalogClass();
            // use CatalogClass The object's Create Method to create ACCESS The database 
            cat.Create(DBPath);
        }
        #endregion
        #region  The compression Access The database 
        /// <summary>
        ///  The compression Access The database 
        /// </summary>
        /// <param name="DBPath"> Absolute database path </param>
        public static void CompactAccess(string DBPath)
        {
            if (!File.Exists(DBPath))
            {
                throw new Exception(" The target database does not exist , Unable to compress ");
            }

            // Declare a temporary database name 
            string temp = DateTime.Now.Year.ToString();
            temp += DateTime.Now.Month.ToString();
            temp += DateTime.Now.Day.ToString();
            temp += DateTime.Now.Hour.ToString();
            temp += DateTime.Now.Minute.ToString();
            temp += DateTime.Now.Second.ToString() + ".bak";
            temp = DBPath.Substring(0, DBPath.LastIndexOf("\\") + 1) + temp;
            // Defines the connection string for the temporary database 
            string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+temp;
            // Defines the connection string for the target database 
            string DBPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPath;
            // create 1 a JetEngineClass Instance of an object 
            JRO.JetEngineClass jt = new JRO.JetEngineClass();
            // use JetEngineClass The object's CompactDatabase Method compress repair database 
            jt.CompactDatabase(DBPath2, temp2);
            // Copy the temporary database to the target database ( cover )
            File.Copy(temp, DBPath, true);
            // Finally, the temporary database is deleted 
            File.Delete(temp);
        }
        #endregion
        #region  The backup Access The database 
        /// <summary>
        ///  The backup Access The database 
        /// </summary>
        /// <param name="srcPath"> Absolute path to the database to be backed up </param>
        /// <param name="aimPath"> Absolute path to the database backed up to </param>
        /// <returns></returns>
        public static void Backup(string srcPath,string aimPath)
        {

            if (!File.Exists(srcPath))
            {
                throw new Exception(" The source database does not exist , Unable to backup ");
            }
            try
            {
                File.Copy(srcPath,aimPath,true);
            }
            catch(IOException ixp)
            {
                throw new Exception(ixp.ToString());
            }

        }
        #endregion
        #region  reduction Access The database 
        /// <summary>
        ///  reduction Access The database 
        /// </summary>
        /// <param name="bakPath"> Absolute path to the backup database </param>
        /// <param name="dbPath"> The absolute path of the database to restore </param>
        public static void RecoverAccess(string bakPath,string dbPath)
        {          
            if (!File.Exists(bakPath))
            {
                throw new Exception(" The backup database does not exist , Unable to restore ");
            }
            try
            {
                File.Copy(bakPath, dbPath, true);
            }
            catch (IOException ixp)
            {
                throw new Exception(ixp.ToString());
            }       
        }       
        #endregion
    }
}


Related articles: