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
- -- Backup database
- Backup database db_CSManage to disk = 'C: \ backup. Bak'
- -- To restore a database, you must first back up the log file of the database to the original backup file.
- 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
- 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
- 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 .. sysprocesses 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 (); // force stop a user process
- Cmd. ExecuteNonQuery ();
- }
- Conn. 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 ();
- Sqlcon. 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 );
- }
The source code of the entire invoicing system is attached.
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.