* <Br/> * Function Description: backs up and recovers SQL Server databases. <br/> * when using SQL Server, reference sqldmo in the COM component. DLL components <br/> * when using access, browse and add reference to the following two DLL <br/> * Reference C:/program files/common files/system/ADO/msadox. DLL, which contains the ADOX namespace <br/> * References C:/program files/common files/system/ADO/msjro. DLL, this dll contains the jro namespace <br/> ****************************** **************************************** * *********/<br/> using system; <br/> using system. data; <B R/> using system. configuration; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. web. UI. htmlcontrols; <br/> using system. io; <br/> using ADOX; // This namespace contains the class (method) for creating access -- Solution ==> reference ==> add reference ==> find the tour. DLL <br/> using jro; // The namespace contains the class (method) for compressing access. <br/> namespace EC <br/> {<br />/// <Summary> <br/> // database recovery and backup <br/> /// </Summary> <br/> public class sqlbackobject <br/ >{< br/> Public sqlbackobject () <br/> {<br/> // todo: add the constructor logic here <br/> // <br/>}< br/> # region SQL database backup <br/> /// <summary> <br/>/ // SQL database backup <br/> /// </Summary> <br/> /// <Param name = "serverip"> SQL Server IP address or (localhost) </param> <br/> /// <Param name = "loginname"> database logon name </param> <br/> // <Param Name = "loginpass"> database logon password </param> <br/> // <Param name = "dbname"> database name </param> <br/> // /<Param name = "backpath"> backup path </param> <br/> Public static void sqlback (string serverip, string loginname, string loginpass, string dbname, string backpath) <br/>{< br/> sqldmo. backup obackup = new sqldmo. backupclass (); <br/> sqldmo. sqlserver osqlserver = new sqldmo. sqlserverclass (); <br/> try <br/>{< br/> osqlserv Er. loginsecure = false; <br/> osqlserver. connect (serverip, loginname, loginpass); <br/> obackup. database = dbname; <br/> obackup. files = backpath; <br/> obackup. backupsetname = dbname; <br/> obackup. backupsetdescription = "database backup"; <br/> obackup. initialize = true; <br/> obackup. sqlbackup (osqlserver); <br/>}< br/> catch (exception e) <br/>{< br/> throw new exception (E. tostring (); <br/>}< br/> fi Nally <br/> {<br/> osqlserver. disconnect (); <br/>}< br/> # endregion <br/> # region SQL Restore database <br/> /// <summary> <br/>/ // SQL Restore database <br/> /// </Summary> <br/> /// <Param name = "serverip"> SQL Server IP address or (localhost) </param> <br/> /// <Param name = "loginname"> database logon name </param> <br/> /// <Param name = "loginpass"> database logon password </param> <br/> // <Param name = "dbname"> name of the database to be restored </param> <br/> // <Param name = "backpat H "> database backup path </param> <br/> Public static void sqldbrestore (string serverip, string loginname, string loginpass, string dbname, string backpath) <br/>{</P> <p> sqldmo. restore orestore = new sqldmo. restoreclass (); <br/> sqldmo. sqlserver osqlserver = new sqldmo. sqlserverclass (); <br/> try <br/>{< br/> osqlserver. loginsecure = false; <br/> osqlserver. connect (serverip, loginname, loginpass); <br/> orestore. Action = sqldmo. sqldmo_restore_type.sqldmorestore_database; <br/> orestore. database = dbname; <br/> orestore. files = backpath; <br/> orestore. filenumber = 1; <br/> orestore. replacedatabase = true; <br/> orestore. sqlrestore (osqlserver); <br/>}< br/> catch (exception e) <br/>{< br/> throw new exception (E. tostring (); <br/>}< br/> finally <br/>{< br/> osqlserver. disconnect (); <br/>}< br //> # Endregion <br/> # region creates an Access database based on the specified file name <br/> /// <summary> <br/> // Based on the specified file name create data <br/> /// </Summary> <br/> // <Param name = "dbpath"> absolute path + file name </param> <br/> Public static void createaccess (string dbpath) <br/>{< br/> If (file. exists (dbpath) // check whether the database already exists <br/>{< br/> throw new exception ("the target database already exists and cannot be created "); <br/>}< br/> dbpath = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ dbpath; <br // Create a catalogclass object instance <br/> ADOX. catalogclass cat = new ADOX. catalogclass (); <br/> // use the create method of the catalogclass object to create an Access database <br/> cat. create (dbpath ); <br/>}< br/> # endregion <br/> # region compressed Access database <br/> /// <summary> <br/> // compressed Access Database <br/> /// </Summary> <br/> // <Param name = "dbpath"> absolute database path </param> <br/> Public static void compactaccess (string dbpath) <br/>{< br/> If (! File. exists (dbpath) <br/>{< br/> throw new exception ("the target database does not exist and cannot be compressed "); <br/>}</P> <p> // declare the temporary database name <br/> string temp = datetime. now. year. tostring (); <br/> temp + = datetime. now. month. tostring (); <br/> temp + = datetime. now. day. tostring (); <br/> temp + = datetime. now. hour. tostring (); <br/> temp + = datetime. now. minute. tostring (); <br/> temp + = datetime. now. second. tostring () + ". bak "; <br/> temp = dbpat H. substring (0, dbpath. lastindexof ("//") + 1) + temp; <br/> // define the connection string of the temporary database <br/> string temp2 = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ temp; <br/> // define the connection string of the target database <br/> string dbpath2 =" provider = Microsoft. jet. oledb.4.0; Data Source = "+ dbpath; <br/> // create an instance of the jetengineclass object <br/> jro. jetengineclass Jt = new jro. jetengineclass (); <br/> // use the compactdatabase method of the jetengineclass object to compact the restoration database. <br/> JT. Compactdatabase (dbpath2, temp2); <br/> // copy the temporary database to the target database (overwrite) <br/> file. copy (temp, dbpath, true); <br/> // Delete the temporary database. <br/> file. delete (temp ); <br/>}< br/> # endregion <br/> # region backup Access database <br/> /// <summary> <br/> // backup Access Database <br/> /// </Summary> <br/> /// <Param name = "srcpath"> absolute path of the database to be backed up </param> <br/> /// <Param name = "aimpath"> absolute path of the database to be backed up </param> <br/> /// <returns> </returns> <br/> Public Static void backup (string srcpath, string aimpath) <br/>{</P> <p> If (! File. exists (srcpath) <br/>{< br/> throw new exception ("the source database does not exist and cannot be backed up "); <br/>}< br/> try <br/> {<br/> file. copy (srcpath, aimpath, true); <br/>}< br/> catch (ioexception IXP) <br/>{< br/> throw new exception (IXP. tostring ()); <br/>}</P> <p >}< br/> # endregion <br/> # region restore access database <br/> // <summary> <br />/// restore the ACCESS database <br/> /// </Summary> <br/> /// <Param name = "bakpath"> absolute path of the backup database </param> <B R/> // <Param name = "dbpath"> absolute path of the database to be restored </param> <br/> Public static void recoveraccess (string bakpath, string dbpath) <br/>{< br/> If (! File. exists (bakpath) <br/>{< br/> throw new exception ("the backup database does not exist and cannot be restored "); <br/>}< br/> try <br/> {<br/> file. copy (bakpath, dbpath, true); <br/>}< br/> catch (ioexception IXP) <br/>{< br/> throw new exception (IXP. tostring ()); <br/>}< br/> # endregion <br/>}< br/> The following figure shows the website background" backup: <br/> protected void button#click (Object sender, eventargs e) <br/>... {<br/> string newname = "Webjake" + datetime. now. year. tostring () + datetime. now. month. tostring () + datetime. now. day. tostring () + datetime. now. hour. tostring () + ". bak "; <br/> sqlconnection Cn = dB. createcon (); <br/> string nepath = server. mappath (".. /.. /databack/") + newname; <br/> string SQL =" backup database webjake to disk = '"+ nepath + "'"; <br/> sqlcommand cmd = new sqlcommand (SQL, CN); <br/> try <br/>... {<br/> CN. open (); <br/> Cmd. executenonquery (); <br/> hyperlink1.text = "successfully backed up to the server path:" + nepath + "click to download to local! "; <Br/> hyperlink1.navigateurl = ".. /.. /databack/"+ newname; <br/> hyperlink1.visible = true; <br/>}< br/> catch (exception ex) <br/>... {<br/> string exm = ex. message; <br/> label1.text = "An error occurred while backing up the database. This file may not exist! "; <Br/> label1.visible = true; <br/>}< br/> finally <br/>... {<br/> cmd. dispose (); <br/> CN. close (); <br/> CN. dispose (); <br/>}< br/> of course, this is the most critical sentence: <br/> backup database webjake to disk = 'path to save'