zoukankan      html  css  js  c++  java
  • SQL Server 数据库备份

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

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

      (1)完全备份

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

      (2)事务日志备份

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

      (3)差异备份

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

      (4)文件和文件组备份

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

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

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

    BACKUP DATABASE {database_name|@database_name_var}
    TO<backup_device>[,...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)或字符串

    数据类型(ntext或text数据类型除外)的变量

    <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)或字符串数据类型(ntext或text数据类型除外)

    的变量。

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

    追加到该设备

     注意:当指定 TO DISK 或 TO TAPE 时,请输入完整路径和文件名。例如,DISK='C:Program FilesMicrosoft SQL ServerMSSQLBACKUPackup.dat'。

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

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

    程序的主要代码如下:

    Frm_Main.cs:

     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 BackUpDataBase
    13 {
    14     public partial class Frm_Main : Form
    15     {
    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             try
    44             {
    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                     else
    66                     {
    67                         MessageBox.Show("请重新命名!");//弹出消息对话框
    68                     }
    69                 }
    70             }
    71             catch (Exception k)
    72             {
    73                 MessageBox.Show(k.Message);//弹出消息对话框
    74             }
    75         }
    76     }
    77 }
    View Code

    Frm_Main.designer.cs:

      1 namespace BackUpDataBase
      2 {
      3     partial class Frm_Main
      4     {
      5         /// <summary>
      6         /// 必需的设计器变量。
      7         /// </summary>
      8         private System.ComponentModel.IContainer components = null;
      9 
     10         /// <summary>
     11         /// 清理所有正在使用的资源。
     12         /// </summary>
     13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
     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         /// <summary>
     26         /// 设计器支持所需的方法 - 不要
     27         /// 使用代码编辑器修改此方法的内容。
     28         /// </summary>
     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         #endregion
    103 
    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 }
    View Code

    下载地址:http://dl.vmall.com/c02bvayygk

  • 相关阅读:
    1. Deep Q-Learning
    Ⅶ. Policy Gradient Methods
    Ⅴ. Temporal-Difference Learning
    idea在service窗口中显示多个服务
    pycharm下运行flask框架的脚本时报错
    windows下部署 flask (win10+flask+nginx)
    pip install selenium报错解决方法
    pip 及 selenium安装命令
    动作捕捉系统用于模仿学习
    柔性微创手术机器人性能实验验证
  • 原文地址:https://www.cnblogs.com/gates/p/3835393.html
Copyright © 2011-2022 走看看