Back up and restore databases in ASP. NET and asp.net

Source: Internet
Author: User

Back up and restore databases in ASP. NET and asp.net
I read the inventory management system in "C # project real-time recording" yesterday. Like the case in other books, it is nothing more than adding, deleting, querying, and modifying databases, however, this invoicing system has a function to back up and restore the database, which is quite interesting. After reading the code, it turns out that the database is backed up and restored through SQL statements, the SQL statement is as follows:
SQL code

  1. -- Backup database
  2. Backup database db_CSManage to disk = 'C: \ backup. Bak'
  3. -- To restore a 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'

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
  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 successful! "," Prompt box ", MessageBoxButtons. OK, MessageBoxIcon. Information );
  12. This. Close ();
  13. }
  14. Else
  15. {
  16. MessageBox. Show ("data backup failed! "," Prompt box ", MessageBoxButtons. OK, MessageBoxIcon. Information );
  17. }
  18. }
  19. Else
  20. {
  21. MessageBox. Show ("enter the correct backup location and file name! "," Prompt box ", MessageBoxButtons. OK, MessageBoxIcon. Information );
  22. } // End
  23. }
  24. Catch (Exception ee)
  25. {
  26. MessageBox. Show (ee. Message. ToString ());
  27. }

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
  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 connected 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 (); // force stop a 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 restored successfully! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information );
  37. MessageBox. Show ("to avoid data loss, the entire system is disabled after the database is restored. ");
  38. Application. Exit ();
  39. }
  40. Else
  41. {
  42. MessageBox. Show ("select backup file! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Warning );
  43. }

The source code of the entire invoicing system is attached.
  • 08. rar (5.8 MB)

Aspnet implements database backup and Restoration

Backup: 133

How to write the backup and restoration database code in ASPNET

Both backup and restoration are implemented by calling SQL statements through ADO. NET. But you browse... If it is an ASP. NET project, it seems inappropriate. Permission conflict.
 

Related Article

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.