Backing up and restoring Databases in ASP.

Source: Internet
Author: User

Yesterday read the "C # project Record" in the invoicing management system, and other books in the same case, nothing more than the database additions and deletions to check and change, but this invoicing system has a backup and restore the function of the database, quite interested, looked at the code, so to speak, That is, by using SQL statements to back up and restore the database, the SQL statements are as follows:
SQL code
    1. --Backing up the database
    2. Backup database db_csmanage to disk=' C:\backup.bak '
    3. --To restore the database, you must first back up the log file of the database to the original backup file
    4. Backup LOG db_csmanage to disk=' C:\backup.bak '
    5. Restore database db_csmanage from disk=' C:\backup.bak '

Where Db_csmanage is the database name, the path after disk is the path to the backup file store.
Know the SQL statement, then in the. NET code is much easier to back up. NET code is as follows:
C # code
  1. Try
  2. {
  3. if (txtpath.text! = "" && txtname122.text! = "")
  4. {
  5. Getsqlconnection Gecon = new Getsqlconnection ();
  6. SqlConnection con = Gecon.getcon ();
  7. string strbacl = "BACKUP database db_csmanage to disk= '" + txtPath.Text.Trim () + "\ \" + TxtName.Text.Trim () +  ". Bak";
  8. SqlCommand CMD = new SqlCommand (Strbacl, con);
  9. if (cmd.executenonquery ()! = 0)
  10. {
  11. MessageBox.Show ("Data backup succeeded!  ", " hint box ", MessageBoxButtons.OK, MessageBoxIcon.Information);
  12. This .  Close ();
  13. }
  14. Else
  15. {
  16. MessageBox.Show ("Data backup failed!  ", " hint box ", MessageBoxButtons.OK, MessageBoxIcon.Information);
  17. }
  18. }
  19. Else
  20. {
  21. MessageBox.Show ("Please fill in the correct location and file name of the backup!")  ", " hint box ", MessageBoxButtons.OK, MessageBoxIcon.Information);
  22. }//End
  23. }
  24. catch (Exception ee)
  25. {
  26. MessageBox.Show (EE. Message.tostring ());
  27. }

The following is the code to restore the database, in the example found that the first to remove the process related to the database, and then before the restore must be the database log back to the start of the backup file before restoring the backup file, or error, the code is as follows:
C # code
  1. if (textpaht.text! = "")
  2. {
  3. Getsqlconnection Gecon = new Getsqlconnection ();
  4. SqlConnection con = Gecon.getcon ();
  5. if (con. state = = ConnectionState.Open)
  6. {
  7. Con. Close ();
  8. }
  9. string datestr = "Data source=niunan\\sqlexpress;database=master; User Id=sa;  pwd=123456 ";
  10. SqlConnection conn = new SqlConnection (DATESTR);
  11. Conn. Open ();
  12. //-------------------Kill all processes that connect to the Db_csmanage database--------------
  13. string strSQL = "select spid from Master."  sysprocesses where dbid=db_id (' db_csmanage ') ";
  14. SqlDataAdapter Da = New SqlDataAdapter (strSQL, Conn);
  15. DataTable spidtable = new DataTable ();
  16. Da.fill (spidtable);
  17. SqlCommand CMD = new SqlCommand ();
  18. Cmd.commandtype = CommandType.Text;
  19. Cmd.connection = conn;
  20. For (int iRow = 0; IRow <= spidtable.rows.count-1; irow++)
  21. {
  22. Cmd.commandtext = "Kill" + spidtable.rows[irow][0].   ToString (); //Forcibly close user process
  23. Cmd.executenonquery ();
  24. }
  25. Conn. Close ();
  26. Conn. Dispose ();
  27. //--------------------------------------------------------------------  
  28. SqlConnection Sqlcon = new SqlConnection (DATESTR);
  29. Sqlcon. Open ();
  30. String sql = "BACKUP log db_csmanage to disk= '" + textPaht.Text.Trim () + "' Restore Database Db_csmanage from  disk= ' "+ textPaht.Text.Trim () + " ' ";
  31. SqlCommand SQLCMD = new SqlCommand (SQL, Sqlcon);
  32. Sqlcmd.executenonquery ();
  33. Sqlcmd.dispose ();
  34. Sqlcon. Close ();
  35. Sqlcon. Dispose ();
  36. MessageBox.Show ("Data restore succeeded!")  ", " hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
  37. MessageBox.Show ("to avoid data loss, the entire system will be shut down after a database restore.")  ");
  38. Application.exit ();
  39. }
  40. Else
  41. {
  42. MessageBox.Show ("Please select a backup file!  ", " hint ", MessageBoxButtons.OK, messageboxicon.warning);
  43. }

Attached to the entire invoicing system source code
    • 08.rar (5.8 MB)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.