This article mainly introduces the method example of asp.net backup and restore database, the friend who needs can refer to the following
Code as follows:/********************************************************************************** * * Feature Description: Backup and restore SQL Server database * author: Liu Gongxun; * version: V0.1 (c#2.0); Time: 2007-1-1 * When you are using SQL Server, refer to the SQLDMO.dll component in the COM component * when you use Access, browse to add references to the following two DLLs & nbsp;* Reference C:Program FilesCommon FilesSystemadomsadox.dll, this DLL contains ADOX namespaces * references C:Program FilesCommon FilesSystemadomsjro.dll, which contains JRO namespaces * /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 classes that create access (method)--Solution ==> Reference ==> Add Reference ==> Tour find the. dll using jro;//This namespace contains classes (methods) for compressing access n Amespace EC { ///<summary> ///database recovery and backup ///</summary> public class Sqlbackobject { public sqlbackobject () { &NB Sp //TODO: Add constructor logic here // &NBS P } #region SQL database backup ///<summary> ///SQL database backup ///</summary> ///<param Name= "ServerIP" >sql server IP or (Localhost) </param> ///<param name= "LoginName" > Database login </param> ///<param name= "loginpass" > Database login password </param> & nbsp ///<param name= "dbname" > Database name </param> ///<param name= "Backpath" > Path to backup </param> public static void Sqlback (String serverip,string loginname,string loginpass,string dbname,string Ba Ckpath) { SQLDMO. Backup obackup = new SQLDMO. Backupclass (); SQLDMO. SQL Server oSQLServer = new SQLDMO. Sqlserverclass (); try { &NBS P osqlserver.loginsecure = false; Osqlserver.connect (ServerIP, LoginName, Loginpass); obackup.database = dbname; obackup.files = Backpath; obackup.backupsetname = dbname; obackup.backupsetdescription = "Database Backup"; &NBSP obackup.initialize = true; obackup.sqlbackup (oSQLServer); &N catch (Exception e) Bsp { throw new Exception (e.tostring ()); nbsp { finally { Osqlserver.disconnect () &NBS P { #endregion #region S QL Recovery database ///<summary> ///SQL Recovery database &NBS P </summary> ///<param name= "ServerIP" >sql server IP or (Localhost) </param> ///<param name= "LoginName" > Database login </param> ///<param name= "loginpass" > Database login password </param> ///<param Nam E= "dbname" > database name to be restored </param> ///<param name= "Backpath" > Database backup Path </param > public static void Sqldbrestore (String serverip,string loginname,string Loginpass, String dbname,string backpath) { SQLDMO. Restore Orestore = new SQLDMO. Restoreclass (); SQLDMO. SQL Server oSQLServer = new SQLDMO. Sqlserverclass (); try { &NBS P 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); &N catch (Exception e) Bsp { throw new Exception (e.tostring ()); nbsp { finally { &NBSp Osqlserver.disconnect (); { #endre Gion #region Create an Access database according to a specified file name ///<summary>   ; ///Create data according to the specified file name ///</summary> ///&L T;param name= "DBPath" > Absolute path + file name </param> public static void Createaccess (string DBPath) { if (file.exists (dbpath))/Check whether the database exists   ; { throw new Exception ("Target database already exists") , cannot be created "); D Bpath = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +dbpath; &NBSP //Create a Catalogclass object instance ADOX. Catalogclass cat = new ADOX. Catalogclass (); //Create an Access database by using the Catalogclass object creation method Cat. Create (DBPath); { #endregion #region Compression A ccess database ///<summary> ///compression Access database ///</summary> ///<param name= "DBPath" > Database absolute path </param> &NBSP ; public static void Compactaccess (String dbpath) { &N Bsp IF (! File.exists (DBPath)) { T Hrow New Exception ("destination database does not exist, cannot be compressed"); } &NBSp //Declaration of 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; //define temporary database connection string String temp2 = "Provid Er=microsoft.jet.oledb.4.0;data source= "+TEMP; //define target database connection string string DBPath2 = "Prov Ider=microsoft.jet.oledb.4.0;data source= "+dbpath; &NBSp //Create an instance of a Jetengineclass object JRO . Jetengineclass JT = new JRO. Jetengineclass (); //Using Jetengineclass object CompactDatabase method Compression Repair Database &NBSP ; Jt.compactdatabase (DBPath2, TEMP2); //Copy temp database to target database (overwrite) file.copy (temp, DB Path, True); //FINAL Delete temporary database File.delete (temp); #endregion #region back up access data Library ///<summary> ///backup Access database </summary> ///<param name= "Srcpath" > Database absolute path to back up </param> ///<param name= "Aimpath" > Backup to Database absolute path </param> &nbsP ///<returns></returns> public static void Backup (String SRCP Ath,string Aimpath) { if (! File.exists (Srcpath)) { throw new Exception ("The source database does not exist, cannot be backed up"); try &NBS P { file.copy (srcpath,aimpath,true); &NB Sp } catch (IOException ixp) { throw new Exception (IXP. ToString ()); #endre Gion &NBsp #region Restore Access database ///<summary> ///restore access Database ///</summary> ///<param name= "Bakpath" > Backup Database Absolute Road Dia </param> ///<param name= "DBPath" > Database absolute path to restore </param> public static void Recoveraccess (String bakpath,string dbpath) { & nbsp if (! File.exists (Bakpath)) { throw new Exception ("Backup database does not exist, cannot be restored"); try &NBS P { file.copy (Bakpath, DBPath, true); &N Bsp } &NBSP; catch (IOException ixp) { &NBSP ; throw new Exception (IXP. ToString ()); & nbsp #endregion }}