SQL code:
-- Backup database db_csmanage to disk = 'C: \ backup. bak '-- to restore the database, you must first back up the log file of the database to the original backup file to backup log db_csmanage to disk = 'C: \ backup. bak 'Restore database db_csmanage from disk = 'C: \ backup. bak'
Db_csmanage indicates the database name, and the path after disk is the path of the backup file storage.
Knowing the SQL statements makes it easier to use. Net code. The backup. Net code is as follows:
C # code-Backup:
Try {If (txtpath. Text! = "" & Txtname122.text! = "") {Getsqlconnection gecon = new getsqlconnection (); sqlconnection con = gecon. getcon (); string strbacl = "backup database db_csmanage to disk = '" + txtpath. text. trim () + "\" + txtname. text. trim () + ". bak '"; sqlcommand cmd = new sqlcommand (strbacl, con); If (CMD. executenonquery ()! = 0) {MessageBox. Show ("Data Backup successful! "," Prompt box ", messageboxbuttons. OK, messageboxicon. Information); this. Close () ;}else {MessageBox. Show (" data backup failed! "," Prompt box ", messageboxbuttons. OK, messageboxicon. Information) ;}} else {MessageBox. Show (" enter the correct backup location and file name! "," Prompt box ", messageboxbuttons. OK, messageboxicon. Information);} // end} catch (exception ee) {MessageBox. Show (EE. Message. tostring ());}
The following is the database restoration code. In this example, we found that the process related to the database must be deleted first, and then the database logs must be backed up to the backup file before restoration, then restore the backup file. Otherwise, an error occurs. The Code is as follows:
C # code-restore:
If (textpaht. Text! = "") {Getsqlconnection gecon = new getsqlconnection (); sqlconnection con = gecon. getcon (); If (con. state = connectionstate. open) {con. close () ;}string datestr = "Data Source = niunan \ sqlexpress; database = Master; user id = sa; Pwd = 123456"; sqlconnection conn = new sqlconnection (datestr ); conn. open (); // ------------------- kill all processes connected to the db_csmanage database ------------ string strsql = "select spid from master .. sysprocess Es where dbid = db_id ('db _ csmanage') "; sqldataadapter da = new sqldataadapter (strsql, Conn); datatable spidtable = new datatable (); DA. fill (spidtable); sqlcommand cmd = new sqlcommand (); cmd. commandtype = commandtype. text; cmd. connection = conn; For (INT irow = 0; irow <= spidtable. rows. count-1; irow ++) {cmd. commandtext = "kill" + spidtable. rows [irow] [0]. tostring (); // forcibly shut down the user process cmd. executenonquery ();} Co Nn. close (); Conn. dispose (); // ---------------------------------------------------------------------- sqlconnection sqlcon = new sqlconnection (datestr); sqlcon. open (); string SQL = "backup log db_csmanage to disk = '" + textpaht. text. trim () + "'Restore database db_csmanage from disk = '" + textpaht. text. trim () + "'"; sqlcommand sqlcmd = new sqlcommand (SQL, sqlcon); sqlcmd. executenonquery (); sqlcmd. dispose (); SQL Con. Close (); sqlcon. Dispose (); MessageBox. Show ("Data restored successfully! "," Prompt ", messageboxbuttons. OK, messageboxicon. Information); MessageBox. Show (" to avoid data loss, the entire system is disabled after the database is restored. "); Application. Exit () ;}else {MessageBox. Show (" select backup file! "," Prompt ", messageboxbuttons. OK, messageboxicon. Warning );}
From: http://niunan.javaeye.com/blog/321062