日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

SQL Server 数据库备份

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL Server 数据库备份 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SQL Server 數據庫備份

原文 http://www.cnblogs.com/ynbt/archive/2013/04/04/2999642.html

  備份數據庫是指對數據庫或事務日志進行復制,當系統、磁盤或數據庫文件損壞時,可以使用備份文件進行恢復,防止數據丟失。

  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)或字符串

數據類型(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 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 }

Frm_Main.designer.cs:

View Code
  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 }

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

作者:Crazy大象
出處:http://www.cnblogs.com/ynbt/
關于作者:專注于.Net、WCF和移動互聯網開發。
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,可以通過ffy_wang@qq.com聯系我,非常感謝。 。 posted on 2013-04-25 15:47 NET未來之路 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/lonelyxmas/archive/2013/04/25/3042701.html

總結

以上是生活随笔為你收集整理的SQL Server 数据库备份的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。