SQL Server数据库备份/恢复

       SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)封装 Microsoft SQL Server 2000 数据库中的对象。SQL-DMO 允许用支持自动化或 COM 的语言编写应用程序,以管理 SQL Server 安装的所有部分。SQL-DMO 是 SQL Server 2000 中的 SQL Server 企业管理器所使用的应用程序接口 (API);因此使用 SQL-DMO 的应用程序可以执行 SQL Server 企业管理器执行的所有功能。

       SQL-DMO 用于必须包含 SQL Server 管理的任何自动化或 COM 应用程序,例如:
1.封装 SQL Server 作为自己的数据存储并想尽量减少用户的 SQL Server 管理任务的应用程序。
2.在程序本身并入了专门的管理逻辑的应用程序。
3.想在自己的用户界面中集成 SQL Server 管理任务的应用程序。

       SQLDMO对象来自SQLDMO.dll,SQLDMO.dll是随SQL Server2000一起发布的。SQLDMO.dll自身是一个COM对象,因此,在你的.NET项目里必须先引用它


全部源代码:
ExpandedBlockStart.gifContractedBlock.gifPublic Class FrmMainClass FrmMain
InBlock.gif    
Inherits System.Windows.Forms.Form
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub New()Sub New()
InBlock.gif        
MyBase.New()
InBlock.gif
InBlock.gif        
'该调用是 Windows 窗体设计器所必需的。
InBlock.gif
        InitializeComponent()
InBlock.gif
InBlock.gif        
'在 InitializeComponent() 调用之后添加任何初始化
InBlock.gif

ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
'窗体重写 dispose 以清理组件列表。
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
InBlock.gif        
If disposing Then
InBlock.gif            
If Not (components Is NothingThen
InBlock.gif                components.Dispose()
InBlock.gif            
End If
InBlock.gif        
End If
InBlock.gif        
MyBase.Dispose(disposing)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
'Windows 窗体设计器所必需的
InBlock.gif
    Private components As System.ComponentModel.IContainer
InBlock.gif
InBlock.gif    
'注意: 以下过程是 Windows 窗体设计器所必需的
InBlock.gif
    '可以使用 Windows 窗体设计器修改此过程。
InBlock.gif
    '不要使用代码编辑器修改它。
InBlock.gif
    Friend WithEvents btnRestore As System.Windows.Forms.Button
InBlock.gif    
Friend WithEvents btnBak As System.Windows.Forms.Button
InBlock.gif    
Friend WithEvents cbServer As System.Windows.Forms.ComboBox
InBlock.gif    
Friend WithEvents cbDatabase As System.Windows.Forms.ComboBox
InBlock.gif    
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
InBlock.gif    
Friend WithEvents txtUserName As System.Windows.Forms.TextBox
InBlock.gif    
Friend WithEvents txtPassword As System.Windows.Forms.TextBox
InBlock.gif    
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
InBlock.gif    
Friend WithEvents Label1 As System.Windows.Forms.Label
InBlock.gif    
Friend WithEvents Label2 As System.Windows.Forms.Label
InBlock.gif    
Friend WithEvents Label3 As System.Windows.Forms.Label
InBlock.gif    
Friend WithEvents Label4 As System.Windows.Forms.Label
InBlock.gif    
Friend WithEvents A As System.Windows.Forms.RadioButton
InBlock.gif    
Friend WithEvents B As System.Windows.Forms.RadioButton
ExpandedSubBlockStart.gifContractedSubBlock.gif    
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
InBlock.gif        
Me.btnRestore = New System.Windows.Forms.Button
InBlock.gif        
Me.btnBak = New System.Windows.Forms.Button
InBlock.gif        
Me.cbServer = New System.Windows.Forms.ComboBox
InBlock.gif        
Me.cbDatabase = New System.Windows.Forms.ComboBox
InBlock.gif        
Me.ListBox1 = New System.Windows.Forms.ListBox
InBlock.gif        
Me.txtUserName = New System.Windows.Forms.TextBox
InBlock.gif        
Me.txtPassword = New System.Windows.Forms.TextBox
InBlock.gif        
Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
InBlock.gif        
Me.Label1 = New System.Windows.Forms.Label
InBlock.gif        
Me.Label2 = New System.Windows.Forms.Label
InBlock.gif        
Me.Label3 = New System.Windows.Forms.Label
InBlock.gif        
Me.Label4 = New System.Windows.Forms.Label
InBlock.gif        
Me.A = New System.Windows.Forms.RadioButton
InBlock.gif        
Me.B = New System.Windows.Forms.RadioButton
InBlock.gif        
Me.SuspendLayout()
InBlock.gif        
'
InBlock.gif
        'btnRestore
InBlock.gif
        '
InBlock.gif
        Me.btnRestore.FlatStyle = System.Windows.Forms.FlatStyle.System
InBlock.gif        
Me.btnRestore.Location = New System.Drawing.Point(320256)
InBlock.gif        
Me.btnRestore.Name = "btnRestore"
InBlock.gif        
Me.btnRestore.TabIndex = 0
InBlock.gif        
Me.btnRestore.Text = "恢复"
InBlock.gif        
'
InBlock.gif
        'btnBak
InBlock.gif
        '
InBlock.gif
        Me.btnBak.FlatStyle = System.Windows.Forms.FlatStyle.System
InBlock.gif        
Me.btnBak.Location = New System.Drawing.Point(232256)
InBlock.gif        
Me.btnBak.Name = "btnBak"
InBlock.gif        
Me.btnBak.TabIndex = 1
InBlock.gif        
Me.btnBak.Text = "备份"
InBlock.gif        
'
InBlock.gif
        'cbServer
InBlock.gif
        '
InBlock.gif
        Me.cbServer.Location = New System.Drawing.Point(10416)
InBlock.gif        
Me.cbServer.Name = "cbServer"
InBlock.gif        
Me.cbServer.Size = New System.Drawing.Size(12120)
InBlock.gif        
Me.cbServer.TabIndex = 2
InBlock.gif        
'
InBlock.gif
        'cbDatabase
InBlock.gif
        '
InBlock.gif
        Me.cbDatabase.Location = New System.Drawing.Point(29616)
InBlock.gif        
Me.cbDatabase.Name = "cbDatabase"
InBlock.gif        
Me.cbDatabase.Size = New System.Drawing.Size(9620)
InBlock.gif        
Me.cbDatabase.TabIndex = 3
InBlock.gif        
'
InBlock.gif
        'ListBox1
InBlock.gif
        '
InBlock.gif
        Me.ListBox1.ItemHeight = 12
InBlock.gif        
Me.ListBox1.Location = New System.Drawing.Point(4840)
InBlock.gif        
Me.ListBox1.Name = "ListBox1"
InBlock.gif        
Me.ListBox1.Size = New System.Drawing.Size(344136)
InBlock.gif        
Me.ListBox1.TabIndex = 4
InBlock.gif        
'
InBlock.gif
        'txtUserName
InBlock.gif
        '
InBlock.gif
        Me.txtUserName.Location = New System.Drawing.Point(112224)
InBlock.gif        
Me.txtUserName.Name = "txtUserName"
InBlock.gif        
Me.txtUserName.TabIndex = 5
InBlock.gif        
Me.txtUserName.Text = "sa"
InBlock.gif        
'
InBlock.gif
        'txtPassword
InBlock.gif
        '
InBlock.gif
        Me.txtPassword.Location = New System.Drawing.Point(288224)
InBlock.gif        
Me.txtPassword.Name = "txtPassword"
InBlock.gif        
Me.txtPassword.TabIndex = 6
InBlock.gif        
Me.txtPassword.Text = ""
InBlock.gif        
'
InBlock.gif
        'ProgressBar1
InBlock.gif
        '
InBlock.gif
        Me.ProgressBar1.Location = New System.Drawing.Point(48256)
InBlock.gif        
Me.ProgressBar1.Name = "ProgressBar1"
InBlock.gif        
Me.ProgressBar1.Size = New System.Drawing.Size(16823)
InBlock.gif        
Me.ProgressBar1.TabIndex = 7
InBlock.gif        
'
InBlock.gif
        'Label1
InBlock.gif
        '
InBlock.gif
        Me.Label1.Location = New System.Drawing.Point(4815)
InBlock.gif        
Me.Label1.Name = "Label1"
InBlock.gif        
Me.Label1.Size = New System.Drawing.Size(5623)
InBlock.gif        
Me.Label1.TabIndex = 8
InBlock.gif        
Me.Label1.Text = "服务器:"
InBlock.gif        
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
InBlock.gif        
'
InBlock.gif
        'Label2
InBlock.gif
        '
InBlock.gif
        Me.Label2.Location = New System.Drawing.Point(23215)
InBlock.gif        
Me.Label2.Name = "Label2"
InBlock.gif        
Me.Label2.Size = New System.Drawing.Size(5623)
InBlock.gif        
Me.Label2.TabIndex = 9
InBlock.gif        
Me.Label2.Text = "数据库:"
InBlock.gif        
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
InBlock.gif        
'
InBlock.gif
        'Label3
InBlock.gif
        '
InBlock.gif
        Me.Label3.Location = New System.Drawing.Point(48224)
InBlock.gif        
Me.Label3.Name = "Label3"
InBlock.gif        
Me.Label3.Size = New System.Drawing.Size(5623)
InBlock.gif        
Me.Label3.TabIndex = 10
InBlock.gif        
Me.Label3.Text = "登录名:"
InBlock.gif        
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
InBlock.gif        
'
InBlock.gif
        'Label4
InBlock.gif
        '
InBlock.gif
        Me.Label4.Location = New System.Drawing.Point(224224)
InBlock.gif        
Me.Label4.Name = "Label4"
InBlock.gif        
Me.Label4.Size = New System.Drawing.Size(5623)
InBlock.gif        
Me.Label4.TabIndex = 11
InBlock.gif        
Me.Label4.Text = "密码:"
InBlock.gif        
Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
InBlock.gif        
'
InBlock.gif
        'A
InBlock.gif
        '
InBlock.gif
        Me.A.Location = New System.Drawing.Point(48192)
InBlock.gif        
Me.A.Name = "A"
InBlock.gif        
Me.A.Size = New System.Drawing.Size(13624)
InBlock.gif        
Me.A.TabIndex = 12
InBlock.gif        
Me.A.Text = "Windows 身份验证"
InBlock.gif        
'
InBlock.gif
        'B
InBlock.gif
        '
InBlock.gif
        Me.B.Checked = True
InBlock.gif        
Me.B.Location = New System.Drawing.Point(184192)
InBlock.gif        
Me.B.Name = "B"
InBlock.gif        
Me.B.Size = New System.Drawing.Size(14424)
InBlock.gif        
Me.B.TabIndex = 13
InBlock.gif        
Me.B.TabStop = True
InBlock.gif        
Me.B.Text = "SQL Server 身份验证"
InBlock.gif        
'
InBlock.gif
        'FrmMain
InBlock.gif
        '
InBlock.gif
        Me.AutoScaleBaseSize = New System.Drawing.Size(614)
InBlock.gif        
Me.ClientSize = New System.Drawing.Size(448286)
InBlock.gif        
Me.Controls.Add(Me.B)
InBlock.gif        
Me.Controls.Add(Me.A)
InBlock.gif        
Me.Controls.Add(Me.Label4)
InBlock.gif        
Me.Controls.Add(Me.Label2)
InBlock.gif        
Me.Controls.Add(Me.ProgressBar1)
InBlock.gif        
Me.Controls.Add(Me.txtPassword)
InBlock.gif        
Me.Controls.Add(Me.txtUserName)
InBlock.gif        
Me.Controls.Add(Me.ListBox1)
InBlock.gif        
Me.Controls.Add(Me.cbDatabase)
InBlock.gif        
Me.Controls.Add(Me.cbServer)
InBlock.gif        
Me.Controls.Add(Me.btnBak)
InBlock.gif        
Me.Controls.Add(Me.btnRestore)
InBlock.gif        
Me.Controls.Add(Me.Label1)
InBlock.gif        
Me.Controls.Add(Me.Label3)
InBlock.gif        
Me.MaximizeBox = False
InBlock.gif        
Me.Name = "FrmMain"
InBlock.gif        
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
InBlock.gif        
Me.Text = "数据库备份/恢复"
InBlock.gif        
Me.ResumeLayout(False)
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
InBlock.gif    
Private ServerName As String
InBlock.gif    
Private UserName As String
InBlock.gif    
Private Password As String
InBlock.gif    
Private message As String
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InBlock.gif        
'得到SQL服务器的列表
InBlock.gif
        '必须安装SQL SERVER 2000 SP2 及以上版本
InBlock.gif
        Dim I As Short
InBlock.gif        
Dim sqlApp As New SQLDMO.Application
InBlock.gif        
Dim ServerName As SQLDMO.NameList
InBlock.gif        ServerName 
= sqlApp.ListAvailableSQLServers
InBlock.gif        
For I = 1 To ServerName.Count
InBlock.gif            cbServer.Items.Add(ServerName.Item(I))
InBlock.gif        
Next
InBlock.gif        cbServer.Items.Add(
"(local)")
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub cbServer_SelectedIndexChanged()Sub cbServer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbServer.SelectedIndexChanged
InBlock.gif        
'
InBlock.gif
        '得到指定SQL服务器所有数据库的列表
InBlock.gif
        Try
InBlock.gif            
Dim sqlApp As New SQLDMO.Application
InBlock.gif            
Dim oServer As New SQLDMO.SQLServer
InBlock.gif            
If A.Checked Then
InBlock.gif                oServer.LoginSecure 
= True
InBlock.gif                oServer.Connect(cbServer.Text)
InBlock.gif            
Else
InBlock.gif                oServer.Connect(cbServer.Text, txtUserName.Text, txtPassword.Text)
InBlock.gif            
End If
InBlock.gif            
'
InBlock.gif
            cbDatabase.Items.Clear()
InBlock.gif            
Dim db As SQLDMO.Database
InBlock.gif            
For Each db In oServer.Databases
InBlock.gif                
Me.cbDatabase.Items.Add(db.Name)
InBlock.gif            
Next
InBlock.gif        
Catch ex As Exception
InBlock.gif            
MsgBox(ex.Message, MsgBoxStyle.Information, "系统消息")
InBlock.gif        
End Try
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub cbDatabase_SelectedIndexChanged()Sub cbDatabase_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDatabase.SelectedIndexChanged
InBlock.gif        
Try
InBlock.gif            
Dim I As Short
InBlock.gif            
Dim oServer As New SQLDMO.SQLServer
InBlock.gif            
If A.Checked Then
InBlock.gif                oServer.LoginSecure 
= True
InBlock.gif                oServer.Connect(cbServer.Text)
InBlock.gif            
Else
InBlock.gif                oServer.Connect(cbServer.Text, txtUserName.Text, txtPassword.Text)
InBlock.gif            
End If
InBlock.gif
InBlock.gif            
Dim db As New SQLDMO.Database
InBlock.gif
InBlock.gif            
For I = 1 To oServer.Databases.Count
InBlock.gif                
If oServer.Databases.Item(I, "dbo").Name = "Northwind" Then Exit For
InBlock.gif            
Next
InBlock.gif            
If I > oServer.Databases.Count Then Exit Sub
InBlock.gif
InBlock.gif            db 
= oServer.Databases.Item(I, "dbo")
InBlock.gif
InBlock.gif            ListBox1.Items.Clear()
InBlock.gif            
'得到所有的存储过程
InBlock.gif
            For I = 1 To db.StoredProcedures.Count
InBlock.gif                ListBox1.Items.Add(db.StoredProcedures.Item(I, 
"dbo").Name)
InBlock.gif            
Next
InBlock.gif            
'得到所有的表
InBlock.gif
            For I = 1 To db.Tables.Count
InBlock.gif                ListBox1.Items.Add(db.Tables.Item(I, 
"dbo").Name)
InBlock.gif            
Next
InBlock.gif            
' 得到所有的视图
InBlock.gif
            For I = 1 To db.Views.Count
InBlock.gif                ListBox1.Items.Add(db.Views.Item(I, 
"dbo").Name)
InBlock.gif            
Next
InBlock.gif        
Catch ex As Exception
InBlock.gif            
MsgBox(ex.Message, MsgBoxStyle.Information, "系统消息")
InBlock.gif        
End Try
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
'声明   
InBlock.gif
    Public WithEvents bkps As SQLDMO.Backup
InBlock.gif    
Public WithEvents rps As SQLDMO.Restore
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub btnBak_Click()Sub btnBak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBak.Click
InBlock.gif        
Try
InBlock.gif            
Dim oServer As New SQLDMO.SQLServer
InBlock.gif            oServer.LoginSecure 
= False
InBlock.gif            
If A.Checked Then
InBlock.gif                oServer.LoginSecure 
= True
InBlock.gif                oServer.Connect(cbServer.Text)
InBlock.gif            
Else
InBlock.gif                oServer.Connect(cbServer.Text, txtUserName.Text, txtPassword.Text)
InBlock.gif            
End If                '连接服务器  
InBlock.gif
            Me.Cursor = Windows.Forms.Cursors.WaitCursor
InBlock.gif            bkps 
= CreateObject("SQLDMO.Backup")
InBlock.gif            bkps.Database 
= cbDatabase.Text   '指定需备份的数据库 
InBlock.gif
            bkps.Action = 0
InBlock.gif            bkps.Files 
= "d:\" & cbDatabase.Text & ".bak"  '指定备份文件   
InBlock.gif
            bkps.Initialize = True
InBlock.gif            ProgressBar1.Value 
= 0
InBlock.gif            ProgressBar1.Maximum 
= 100
InBlock.gif            
Me.Cursor = Windows.Forms.Cursors.Default()
InBlock.gif            Application.DoEvents()
InBlock.gif            
Dim mouseCur As Cursor
InBlock.gif            
Me.Cursor = Windows.Forms.Cursors.WaitCursor
InBlock.gif            bkps.SQLBackup(oServer)
InBlock.gif            ProgressBar1.Value 
= 100
InBlock.gif            Application.DoEvents()
InBlock.gif            bkps 
= Nothing
InBlock.gif            
Me.Cursor = Windows.Forms.Cursors.Default()
InBlock.gif            
MsgBox("数据库恢复完成", MsgBoxStyle.Information, "系统消息")
InBlock.gif        
Catch ex As Exception
InBlock.gif            
MsgBox(ex.Message, MsgBoxStyle.Information, "系统消息")
InBlock.gif        
End Try
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub btnRestore_Click()Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
InBlock.gif        
Try
InBlock.gif            
Dim oServer As New SQLDMO.SQLServer
InBlock.gif            oServer.LoginSecure 
= False
InBlock.gif            
If A.Checked Then
InBlock.gif                oServer.LoginSecure 
= True
InBlock.gif                oServer.Connect(cbServer.Text)
InBlock.gif            
Else
InBlock.gif                oServer.Connect(cbServer.Text, txtUserName.Text, txtPassword.Text)
InBlock.gif            
End If                     '连接服务器  
InBlock.gif
            Me.Cursor = Windows.Forms.Cursors.WaitCursor
InBlock.gif            rps 
= CreateObject("SQLDMO.Restore")
InBlock.gif
InBlock.gif            rps.Database 
= cbDatabase.Text   '指定需备份的数据库 
InBlock.gif
            rps.Action = 0
InBlock.gif            rps.Files 
= "d:\" & cbDatabase.Text & ".bak"  '指定备份文件   
InBlock.gif
            'rps.Initialize = True
InBlock.gif
            ProgressBar1.Value = 0
InBlock.gif            ProgressBar1.Maximum 
= 100
InBlock.gif            
Me.Cursor = Windows.Forms.Cursors.Default()
InBlock.gif            Application.DoEvents()
InBlock.gif            
Dim mouseCur As Cursor
InBlock.gif            
Me.Cursor = Windows.Forms.Cursors.WaitCursor
InBlock.gif            rps.SQLRestore(oServer)
InBlock.gif            ProgressBar1.Value 
= 100
InBlock.gif            Application.DoEvents()
InBlock.gif            rps 
= Nothing
InBlock.gif            
Me.Cursor = Windows.Forms.Cursors.Default()
InBlock.gif            
MsgBox("数据库备份完成", MsgBoxStyle.Information, "系统消息")
InBlock.gif        
Catch ex As Exception
InBlock.gif            
MsgBox(ex.Message, MsgBoxStyle.Information, "系统消息")
InBlock.gif        
End Try
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
'显示进度  
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Private Sub bkps_PercentComplete()Sub bkps_PercentComplete(ByVal Message As StringByVal Percent As IntegerHandles bkps.PercentComplete
InBlock.gif        ProgressBar1.Value 
= ProgressBar1.Maximum * (Percent / 100)
ExpandedSubBlockEnd.gif    
End Sub

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub rps_PercentComplete()Sub rps_PercentComplete(ByVal Message As StringByVal Percent As IntegerHandles rps.PercentComplete
InBlock.gif        ProgressBar1.Value 
= ProgressBar1.Maximum * (Percent / 100)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedBlockEnd.gif
End Class

下载源代码:/Files/duanzt/DBManager.rar
None.gif

转载于:https://www.cnblogs.com/duanzt/archive/2006/09/12/501811.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值