Restore an SQL Server database

Source: Internet
Author: User
--- Start of recovery content --- data recovery is particularly important for a good application. Because the data recovery function can recover data to the system when data is damaged, so as to ensure that the system is re-running properly, so as to avoid the loss caused by abnormal data loss. This example shows how to restore a database in C. Restore database

--- Start of recovery content --- data recovery is particularly important for a good application. Because the data recovery function can recover data to the system when data is damaged, so as to ensure that the system is re-running properly, so as to avoid the loss caused by abnormal data loss. This example shows how to restore a database in C. Restore database

--- Restore content start ---

Data Recovery is especially important for an outstanding application. Because the data recovery function can recover data to the system when data is damaged, so as to ensure that the system is re-running properly, so as to avoid the loss caused by abnormal data loss. This example shows how to restore a database in C.

Restoring a database uses the backup file of the database to restore the database. Data loss, integrity, or data errors may occur due to virus damage, disk damage, or operation errors. In this case, you need to restore the database to a certain day, the premise is that data backup must exist.

SQL Server database recovery supports four types, which are used in different scenarios. The following is a brief introduction.

(1) restore the entire database.

(2) full database restoration and differential database restoration. Use the restore database statement to RESTORE the differential backup.

(3) Transaction Log restoration.

(4) Restore individual files and file groups. You can restore files and file groups either by backing up files or file groups, or by backing up the entire database. Transaction logs must be applied to restore files or file groups. In addition, differential file backup can be restored after the complete file is restored.

Note: restoring the database backup will re-create the database and all related files in the database when the backup is complete. However, any database modifications made after the backup file is created will be lost. To restore the transactions that occur after the database backup is created, you must use transaction log backup or differential backup to save the record filing space.

This example uses the SQLDMO. Restore object to Restore the entire database BACKUP using the BACKUP command.

The RESTORE syntax format is as follows:

}] [WITH [RESTRICTED_USER {file_number | @ file_number}] PASSWORD = {password | @ password_variable}] MEDIANAME = {media_name | @ media_name_variable}] MEDIAPASSWORD = {mediapassword | @ signature}] MOVE] KEEP_REPLICATION] {NORECOVERY | RECOVERY | STANDBY = undo_file_name}] {NOREWIND | REWIND}] {NOUNLOAD | UNLOAD}] RESTART] STATS]

Database restoration parameters and descriptions are as follows:

Restore database parameters and descriptions

Parameter description

DATABASE indicates that the entire DATABASE is restored from the backup. If the list of file box file groups is specified, only those files and file groups are restored.

{Database_name | @ database_name_var} is the database that restores logs or the entire database. If it is provided as a variable (@ database_name_var), you can specify this name as a String constant (@ database_name_var = database name) or a string data type (excluding the ntext or text data types) variable

FROM

Specifies the backup device from which the backup is restored. If the FROM clause is not specified, backup and restoration will not occur. The Hong Kong server is rented, but the database is restored. The FROM clause can be omitted to try to restore the database restored through the NORECOVERY option, or switch to a backup server. If the FROM clause is omitted, you must specify

NORECOVERY, RECOVERY, or STANDBY

Specifies the logical or physical backup device used for backup. It can be one or more of the following forms.

{'Logical _ bakcup_device_name '| @ logical_backup_device_name_var}: the logical name of the backup device created by sp_addumpdevice (the database will be restored from the backup device). The name must comply with the identifier rules. If it is used as a variable (@ logical_backup_device_name_var)

Provided, you can specify the variable of the String constant (@ logical_backup_device_name_var = logical_backup_device_name) or string data type (excluding ntext or text data) as the backup device name.

{DISK | TYPE} = 'physical _ backup_device_name '@ physical_backup_device_name_var: allows you to restore a backup from a named DISK or tape device. The device type of a DISK or tape should be specified by the real name of the device (such as the complete path and file name): DISK = 'C: \ Program Files \ Microsoft

SQL Server \ MSSQL \ BACKUP \ Mybackup. dat 'or TYPE =' \. \ tape0 '. If it is specified as a variable (@ physical_backup_device_name_var), the device name can be a String constant (@ physical_backup_device_name_var = 'physical _ backup_device_name ') or a character.

Variable of string data type (except ntext or text data type)

The code for restoring the database is as follows:

+ Dbname ++ path +;

The main code of the program is as follows:

Frm_Main.cs:

View Code

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. text; 7 using System. windows. forms; 8 using System. linq; 9 using System. data. sqlClient; RevertDataBase13 {Frm_Main: Form15 {16 public Frm_Main () 17 {18 InitializeComponent (); 19} Form1_Load (object sender, EventArgs e) 22 {) 25 {SqlDataAdapter da =, con );. cbox_DataBase.DataSource = dt. defaultView ;. cbox_DataBase.DisplayMember = ;. cbox_DataBase.ValueMember =;} 34} button2_Click (object sender, EventArgs e) 37 {38if (this. openFileDialog1.ShowDialog () = DialogResult. OK) 39 {} 42} button#click (object sender, EventArgs e) 45 {} Restore () 50 {dbname = SqlStr1 = +; + dbname + + path +; {cmd. connection = con; cmd. executeNonQuery (); MessageBox. show () ;}67 catch (Exception ex) 68 {); 71 }{} 76} 77} 78} 79}

Frm_Main.designer.cs:

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.