This article describes how to back up and restore a database in asp.net. For more information, see
The Code is as follows: /*************************************** **************************************** * ***** function description: * Author: Liu gongxun; * version: V0.1 (C #2.0); time:-1-1 * when using SQL Server, reference SQLDMO in the COM component. dll component * when using Access, browse to add reference to the following two dll * Reference C: Program FilesCommon FilesSystemadomsadox. dll, which contains the ADOX namespace * References C: Program FilesCommon FilesSystemadomsjro. dll, this DLL contains the JRO 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; // The namespace contains the class (method) for creating ACCESS -- Solution ==> reference ==> add reference ==> find the tour. dllusing JRO; // The namespace contains the class (method) namespace EC for compressing ACCESS {// /<Summary> // database recovery and backup /// </summary> public class SqlBackObject {public SqlBackObject () {// TODO: add the constructor logic //} # region sqldatabase backup // <summary> // sqldatabase backup // </summary> // <param name = "ServerIP "> SQL Server IP address or (Localhost) </param> /// <param name = "LoginName"> database logon name </param> /// <param name = "LoginPass"> database logon password </param>/ // <param name = "DBName"> database name </param> // <param name = "BackPath"> backup path </pa Ram> 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; o Backup. 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 Server IP Address or (Localhost) </param> /// <param name = "LoginName"> database logon name </param> /// <param name = "LoginPass"> database logon Password Code </param> /// <param name = "DBName"> name of the database to be restored </param> /// <param name = "BackPath"> path of 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 an Access database based on the specified file name // <summary> // create data based on the specified file name /// </Summary> /// <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 ("the target database Exists and cannot be created");} DBPath = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ DBPath; // create a CatalogClass object instance ADOX. catalogClass cat = new ADOX. catalogClass (); // use the Create method of the CatalogClass object to Create an ACCESS database cat. create (DBPath) ;}# endregion # regi On compressing the Access database /// <summary> /// compressing the Access 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 and cannot be compressed");} // declare the 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 of the temporary database. String temp2 = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ temp; // defines the connection string of the target database, string DBPath2 =" Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ DBPath; // create an instance of the JetEngineClass object JRO. jetEngineClass jt = new JRO. jetEngineClass (); // use the CompactDatabase method of the JetEngineClass object to compact and fix the database jt. compactDatabase (DBPath2, temp2); // copy the temporary database to the target database (overwrite) File. copy (temp, DBPath, true); // Delete the temporary database File. delete (temp );} # Endregion # region back up the Access database /// <summary> // back up the Access database /// </summary> /// <param name = "srcPath"> absolute database path </param> /// <param name = "aimPath"> absolute path of the database to be backed up </param> /// <returns> </returns> public static void Backup (string srcPath, string aimPath) {if (! File. exists (srcPath) {throw new Exception ("the source database does not exist and cannot be backed up");} try {File. copy (srcPath, aimPath, true);} catch (IOException ixp) {throw new Exception (ixp. toString ());}} # endregion # region restore the Access database /// <summary> /// restore the Access database /// </summary> /// <param name = "bakPath"> backup database absolute path </param> /// <param name = "dbPath"> absolute path of the database to be restored </param> public static void RecoverAccess (string bakPath, string dbPath ){ If (! File. exists (bakPath) {throw new Exception ("the backup database does not exist and cannot be restored");} try {File. copy (bakPath, dbPath, true);} catch (IOException ixp) {throw new Exception (ixp. toString () ;}# endregion }}