博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL Server 数据库备份
阅读量:4638 次
发布时间:2019-06-09

本文共 10016 字,大约阅读时间需要 33 分钟。

原文

  备份数据库是指对数据库或事务日志进行复制,当系统、磁盘或数据库文件损坏时,可以使用备份文件进行恢复,防止数据丢失。

  SQL Server数据库备份支持4种类型,分别应用于不同的场合,下面简要介绍。

  (1)完全备份

  完全备份,即完整数据库备份,可以备份整个数据库,包含用户表、系统表、索引、视图和存储过程等所有数据库对象。这是大多数人常用的方式,但需要花费更多的时间和空间,所以一般推荐一周做一次完全备份。 

  (2)事务日志备份

  事务日志备份时一个单独的文件,记录数据库的改变,备份时只需要复制上次备份以来对数据库所做的改变,可支持从数据库、差异或文件备份中快速恢复,时间少,速度快,推荐每小时甚至更频繁地备份事务日志。

  (3)差异备份

  在完整数据库备份之间执行差异数据备份,比完全备份小,因为只包含自完全备份以来所改变的数据库,优点是存储和恢复速度快。推荐每天做一次差异备份。

  (4)文件和文件组备份

  数据库一般由硬盘上的许多文件构成。如果这个数据库非常大,并且一个晚上也不能备份完,那么可以使用文件和文件组备份,每晚备份数据库的一部分。由于一般情况下数据库不会大到必须使用多个文件存储,所以此种备份并不常用。

  本实例运用SQLDMO.backup对象完成整个系统数据库的备份。这使得在系统或数据库发生故障(如硬盘发生故障)时可以重建系统。

  备份整个数据库的语法如下:

BACKUP DATABASE {database_name|@database_name_var}TO
[,...n][WITH [BLOCKSIZE={blocksize|@blocksize_variable}] [[,]DESCRIPTION={
'text'|@text_variable}] [[,]DIFFERENTIAL] [[,]EXPIREDATE={date|@date_var} |RETAINDAYS={days|@days_var}] [[,]PASSWORD={password|@password_variable}] [[,]FORMAT|NOFORMAT] [[,]{INIT|NOINIT}] [[,]MEDIADESCRIPTION={
'text'|@text_variable}] [[,]MEDIANAME={media_name|@media_name_variable}] [[,]MEDIAPASSWORD={mediapassword|@mediapassword_variable}] [[,]NAME={backup_set_name|@backup_set_name_var}] [[,]{NOSKIP|SKIP}] [[,]{NOREWIND|REWIND}] [[,]{NOUNLOAD|UNLOAD}] [[,]RESTART] [[,]STATS[=percentage]]]

  备份数据库参数及说明如下:

备份数据库参数及说明
参  数   说  明
DATABASE

指定一个完整的数据库备份。加入指定了一个文件盒文件组的列表,那么仅有这些被指定的文件和文件组被备份

{ database_name | @database_name_var }

指定了一个数据库,从该数据库中对事物日志、部分数据库或完整的数据库进行备份。如果作为变了量(database_name_var)提供,则可将该名称指定为字符串常量(@database_name_var=database name)或字符串

数据类型(ntexttext数据类型除外)的变量

<backup_device>

指定备份操作时要使用的逻辑或物理设备。可以是下列一种或多种形式:

{logical_backup_device_name}|{@logical_backup_device_name_var}:是由 sp_addupmdevice创建的备份设备的逻辑名称,数据库将备份到该设备中,其名称必须遵守标识符规则。如果将其作为变量(@logical_backup_device_name_var)提供,

则可将该备份设备名称指定为字符串常量(@logical_backup_device_name_var)提供,则可将该备份设备名称指定为字符串常量(@logical_backup_device_name_var=logical backup device name)或字符串数据类型(ntexttext数据类型除外)

的变量。

{DISK|TAPE}='physical_backup_device_name'|@physical_backup_device_name_var: 允许在指定的磁盘或磁带上创建备份。在执行BACKUP语句之前不必存在指定的物理设备。如果存在物理设备且BACKUP语句中没有指定INIT选项,则 备份

追加到该设备

 注意:当指定 TO DISK 或 TO TAPE 时,请输入完整路径和文件名。例如,DISK='C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\backup.dat'。

   本实例备份数据库的代码如下:

string SqlStr2 = "backup database " + this.DropDownList1.SelectedValue + "to disk'" + this.TextBox1.Text.Trim() + ".bak'";

程序的主要代码如下:

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.IO; 9 using System.Linq;10 using System.Data.SqlClient;11 12 namespace BackUpDataBase13 {14     public partial class Frm_Main : Form15     {16         public Frm_Main()17         {18             InitializeComponent();19         }20 21         private void Form1_Load(object sender, EventArgs e)22         {23             using (SqlConnection con = new SqlConnection(//创建数据库连接对象24 @"server=.;pwd=123;uid=sa;database=master"))25             {26                 DataTable dt = new DataTable();//创建数据表27                 SqlDataAdapter da = new SqlDataAdapter(//创建数据适配器对象28                     "select name from sysdatabases", con);29                 da.Fill(dt);//填充数据表30                 this.comboBox1.DataSource = dt.DefaultView;//设置数据源31                 this.comboBox1.DisplayMember = "name";//设置显示属性32                 this.comboBox1.ValueMember = "name";//设置实际值33             }34         }35 36         private void button1_Click(object sender, EventArgs e)37         {38             beifenInfo();//备份数据库39         }40 41         public void beifenInfo()42         {43             try44             {45                 sd.InitialDirectory = Application.StartupPath + "\\";//默认路径为D:46                 sd.FilterIndex = 1;//默认值为第一个47                 sd.RestoreDirectory = true;//重新定位保存路径48                 sd.Filter = "备份文件 (*.bak)|*.bak|所有文件 (*.*)|*.*";//设置筛选文件类型49                 if (sd.ShowDialog() == DialogResult.OK)50                 {51                     if (!File.Exists(sd.FileName.ToString()))52                     {53                         SqlConnection con = new SqlConnection();//创建数据库连接对象54                         con.ConnectionString = @"server=.;uid=sa;pwd=123;database='" + this.comboBox1.Text + "'";55                         con.Open();//打开数据连接56                         SqlCommand com = new SqlCommand();//创建命令对象57                         this.textBox1.Text = sd.FileName.ToString();//显示文件路径信息58                         com.CommandText = "BACKUP DATABASE " + this.comboBox1.Text +//设置要执行的SQL语句59                             " TO DISK = '" + sd.FileName.ToString() + "'";60                         com.Connection = con;//设置连接属性61                         com.ExecuteNonQuery();//执行SQL语句62                         con.Close();//关闭数据库连接63                         MessageBox.Show("数据备份成功!");//弹出消息对话框64                     }65                     else66                     {67                         MessageBox.Show("请重新命名!");//弹出消息对话框68                     }69                 }70             }71             catch (Exception k)72             {73                 MessageBox.Show(k.Message);//弹出消息对话框74             }75         }76     }77 }

Frm_Main.designer.cs:

View Code
1 namespace BackUpDataBase  2 {  3     partial class Frm_Main  4     {  5         ///   6         /// 必需的设计器变量。  7         ///   8         private System.ComponentModel.IContainer components = null;  9  10         ///  11         /// 清理所有正在使用的资源。 12         ///  13         /// 如果应释放托管资源,为 true;否则为 false。 14         protected override void Dispose(bool disposing) 15         { 16             if (disposing && (components != null)) 17             { 18                 components.Dispose(); 19             } 20             base.Dispose(disposing); 21         } 22  23         #region Windows 窗体设计器生成的代码 24  25         ///  26         /// 设计器支持所需的方法 - 不要 27         /// 使用代码编辑器修改此方法的内容。 28         ///  29         private void InitializeComponent() 30         { 31             this.sd = new System.Windows.Forms.SaveFileDialog(); 32             this.button1 = new System.Windows.Forms.Button(); 33             this.label1 = new System.Windows.Forms.Label(); 34             this.comboBox1 = new System.Windows.Forms.ComboBox(); 35             this.textBox1 = new System.Windows.Forms.TextBox(); 36             this.label3 = new System.Windows.Forms.Label(); 37             this.SuspendLayout(); 38             //  39             // button1 40             //  41             this.button1.Location = new System.Drawing.Point(218, 68); 42             this.button1.Name = "button1"; 43             this.button1.Size = new System.Drawing.Size(75, 23); 44             this.button1.TabIndex = 0; 45             this.button1.Text = "备份"; 46             this.button1.UseVisualStyleBackColor = true; 47             this.button1.Click += new System.EventHandler(this.button1_Click); 48             //  49             // label1 50             //  51             this.label1.AutoSize = true; 52             this.label1.Location = new System.Drawing.Point(38, 18); 53             this.label1.Name = "label1"; 54             this.label1.Size = new System.Drawing.Size(77, 12); 55             this.label1.TabIndex = 1; 56             this.label1.Text = "操作数据库:"; 57             //  58             // comboBox1 59             //  60             this.comboBox1.FormattingEnabled = true; 61             this.comboBox1.Location = new System.Drawing.Point(122, 15); 62             this.comboBox1.Name = "comboBox1"; 63             this.comboBox1.Size = new System.Drawing.Size(171, 20); 64             this.comboBox1.TabIndex = 2; 65             //  66             // textBox1 67             //  68             this.textBox1.Enabled = false; 69             this.textBox1.Location = new System.Drawing.Point(121, 41); 70             this.textBox1.Name = "textBox1"; 71             this.textBox1.Size = new System.Drawing.Size(172, 21); 72             this.textBox1.TabIndex = 4; 73             //  74             // label3 75             //  76             this.label3.AutoSize = true; 77             this.label3.Location = new System.Drawing.Point(15, 44); 78             this.label3.Name = "label3"; 79             this.label3.Size = new System.Drawing.Size(101, 12); 80             this.label3.TabIndex = 5; 81             this.label3.Text = "备份路径及名称:"; 82             //  83             // Form1 84             //  85             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 86             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 87             this.ClientSize = new System.Drawing.Size(310, 103); 88             this.Controls.Add(this.label3); 89             this.Controls.Add(this.textBox1); 90             this.Controls.Add(this.comboBox1); 91             this.Controls.Add(this.label1); 92             this.Controls.Add(this.button1); 93             this.Name = "Form1"; 94             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 95             this.Text = "备份SQL Server数据库"; 96             this.Load += new System.EventHandler(this.Form1_Load); 97             this.ResumeLayout(false); 98             this.PerformLayout(); 99 100         }101 102         #endregion103 104         private System.Windows.Forms.SaveFileDialog sd;105         private System.Windows.Forms.Button button1;106         private System.Windows.Forms.Label label1;107         private System.Windows.Forms.ComboBox comboBox1;108         private System.Windows.Forms.TextBox textBox1;109         private System.Windows.Forms.Label label3;110     }111 }

下载地址:

作者:
出处:
关于作者:专注于.Net、WCF和移动互联网开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过ffy_wang@qq.com联系我,非常感谢。 。
posted on
2013-04-25 15:47 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/archive/2013/04/25/3042701.html

你可能感兴趣的文章
素数筛法优化
查看>>
installshield 注册dll
查看>>
Sublime Text 3 及Package Control 安装(附上一个3103可用的Key)
查看>>
LTE QCI分类 QoS
查看>>
Get MAC address using POSIX APIs
查看>>
bzoj2120
查看>>
基于uFUN开发板的心率计(一)DMA方式获取传感器数据
查看>>
【dp】船
查看>>
oracle, group by, having, where
查看>>
⑥python模块初识、pyc和PyCodeObject
查看>>
object-c中管理文件和目录:NSFileManager使用方法
查看>>
Kibana:分析及可视化日志文件
查看>>
nodejs pm2使用
查看>>
cocos2d-x 3.10 PageView BUG
查看>>
装饰器的基本使用:用户登录
查看>>
CSS选择器总结
查看>>
mysql中sql语句
查看>>
head/tail实现
查看>>
sql语句的各种模糊查询语句
查看>>
vlc 学习网
查看>>