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项目里必须先引用它
全部源代码:Public Class FrmMainClass FrmMain
Inherits System.Windows.Forms.Form
Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
Public Sub New()Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents btnRestore As System.Windows.Forms.Button
Friend WithEvents btnBak As System.Windows.Forms.Button
Friend WithEvents cbServer As System.Windows.Forms.ComboBox
Friend WithEvents cbDatabase As System.Windows.Forms.ComboBox
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents txtUserName As System.Windows.Forms.TextBox
Friend WithEvents txtPassword As System.Windows.Forms.TextBox
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents A As System.Windows.Forms.RadioButton
Friend WithEvents B As System.Windows.Forms.RadioButton
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.btnRestore = New System.Windows.Forms.Button
Me.btnBak = New System.Windows.Forms.Button
Me.cbServer = New System.Windows.Forms.ComboBox
Me.cbDatabase = New System.Windows.Forms.ComboBox
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.txtUserName = New System.Windows.Forms.TextBox
Me.txtPassword = New System.Windows.Forms.TextBox
Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.A = New System.Windows.Forms.RadioButton
Me.B = New System.Windows.Forms.RadioButton
Me.SuspendLayout()
'
'btnRestore
'
Me.btnRestore.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnRestore.Location = New System.Drawing.Point(320, 256)
Me.btnRestore.Name = "btnRestore"
Me.btnRestore.TabIndex = 0
Me.btnRestore.Text = "恢复"
'
'btnBak
'
Me.btnBak.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnBak.Location = New System.Drawing.Point(232, 256)
Me.btnBak.Name = "btnBak"
Me.btnBak.TabIndex = 1
Me.btnBak.Text = "备份"
'
'cbServer
'
Me.cbServer.Location = New System.Drawing.Point(104, 16)
Me.cbServer.Name = "cbServer"
Me.cbServer.Size = New System.Drawing.Size(121, 20)
Me.cbServer.TabIndex = 2
'
'cbDatabase
'
Me.cbDatabase.Location = New System.Drawing.Point(296, 16)
Me.cbDatabase.Name = "cbDatabase"
Me.cbDatabase.Size = New System.Drawing.Size(96, 20)
Me.cbDatabase.TabIndex = 3
'
'ListBox1
'
Me.ListBox1.ItemHeight = 12
Me.ListBox1.Location = New System.Drawing.Point(48, 40)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(344, 136)
Me.ListBox1.TabIndex = 4
'
'txtUserName
'
Me.txtUserName.Location = New System.Drawing.Point(112, 224)
Me.txtUserName.Name = "txtUserName"
Me.txtUserName.TabIndex = 5
Me.txtUserName.Text = "sa"
'
'txtPassword
'
Me.txtPassword.Location = New System.Drawing.Point(288, 224)
Me.txtPassword.Name = "txtPassword"
Me.txtPassword.TabIndex = 6
Me.txtPassword.Text = ""
'
'ProgressBar1
'
Me.ProgressBar1.Location = New System.Drawing.Point(48, 256)
Me.ProgressBar1.Name = "ProgressBar1"
Me.ProgressBar1.Size = New System.Drawing.Size(168, 23)
Me.ProgressBar1.TabIndex = 7
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(48, 15)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(56, 23)
Me.Label1.TabIndex = 8
Me.Label1.Text = "服务器:"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(232, 15)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(56, 23)
Me.Label2.TabIndex = 9
Me.Label2.Text = "数据库:"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(48, 224)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(56, 23)
Me.Label3.TabIndex = 10
Me.Label3.Text = "登录名:"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(224, 224)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(56, 23)
Me.Label4.TabIndex = 11
Me.Label4.Text = "密码:"
Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'A
'
Me.A.Location = New System.Drawing.Point(48, 192)
Me.A.Name = "A"
Me.A.Size = New System.Drawing.Size(136, 24)
Me.A.TabIndex = 12
Me.A.Text = "Windows 身份验证"
'
'B
'
Me.B.Checked = True
Me.B.Location = New System.Drawing.Point(184, 192)
Me.B.Name = "B"
Me.B.Size = New System.Drawing.Size(144, 24)
Me.B.TabIndex = 13
Me.B.TabStop = True
Me.B.Text = "SQL Server 身份验证"
'
'FrmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(448, 286)
Me.Controls.Add(Me.B)
Me.Controls.Add(Me.A)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.ProgressBar1)
Me.Controls.Add(Me.txtPassword)
Me.Controls.Add(Me.txtUserName)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.cbDatabase)
Me.Controls.Add(Me.cbServer)
Me.Controls.Add(Me.btnBak)
Me.Controls.Add(Me.btnRestore)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Label3)
Me.MaximizeBox = False
Me.Name = "FrmMain"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "数据库备份/恢复"
Me.ResumeLayout(False)
End Sub
#End Region
Private ServerName As String
Private UserName As String
Private Password As String
Private message As String
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'得到SQL服务器的列表
'必须安装SQL SERVER 2000 SP2 及以上版本
Dim I As Short
Dim sqlApp As New SQLDMO.Application
Dim ServerName As SQLDMO.NameList
ServerName = sqlApp.ListAvailableSQLServers
For I = 1 To ServerName.Count
cbServer.Items.Add(ServerName.Item(I))
Next
cbServer.Items.Add("(local)")
End Sub
Private Sub cbServer_SelectedIndexChanged()Sub cbServer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbServer.SelectedIndexChanged
'
'得到指定SQL服务器所有数据库的列表
Try
Dim sqlApp As New SQLDMO.Application
Dim oServer As New SQLDMO.SQLServer
If A.Checked Then
oServer.LoginSecure = True
oServer.Connect(cbServer.Text)
Else
oServer.Connect(cbServer.Text, txtUserName.Text, txtPassword.Text)
End If
'
cbDatabase.Items.Clear()
Dim db As SQLDMO.Database
For Each db In oServer.Databases
Me.cbDatabase.Items.Add(db.Name)
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "系统消息")
End Try
End Sub
Private Sub cbDatabase_SelectedIndexChanged()Sub cbDatabase_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDatabase.SelectedIndexChanged
Try
Dim I As Short
Dim oServer As New SQLDMO.SQLServer
If A.Checked Then
oServer.LoginSecure = True
oServer.Connect(cbServer.Text)
Else
oServer.Connect(cbServer.Text, txtUserName.Text, txtPassword.Text)
End If
Dim db As New SQLDMO.Database
For I = 1 To oServer.Databases.Count
If oServer.Databases.Item(I, "dbo").Name = "Northwind" Then Exit For
Next
If I > oServer.Databases.Count Then Exit Sub
db = oServer.Databases.Item(I, "dbo")
ListBox1.Items.Clear()
'得到所有的存储过程
For I = 1 To db.StoredProcedures.Count
ListBox1.Items.Add(db.StoredProcedures.Item(I, "dbo").Name)
Next
'得到所有的表
For I = 1 To db.Tables.Count
ListBox1.Items.Add(db.Tables.Item(I, "dbo").Name)
Next
' 得到所有的视图
For I = 1 To db.Views.Count
ListBox1.Items.Add(db.Views.Item(I, "dbo").Name)
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "系统消息")
End Try
End Sub
'声明
Public WithEvents bkps As SQLDMO.Backup
Public WithEvents rps As SQLDMO.Restore
Private Sub btnBak_Click()Sub btnBak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBak.Click
Try
Dim oServer As New SQLDMO.SQLServer
oServer.LoginSecure = False
If A.Checked Then
oServer.LoginSecure = True
oServer.Connect(cbServer.Text)
Else
oServer.Connect(cbServer.Text, txtUserName.Text, txtPassword.Text)
End If '连接服务器
Me.Cursor = Windows.Forms.Cursors.WaitCursor
bkps = CreateObject("SQLDMO.Backup")
bkps.Database = cbDatabase.Text '指定需备份的数据库
bkps.Action = 0
bkps.Files = "d:\" & cbDatabase.Text & ".bak" '指定备份文件
bkps.Initialize = True
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
Me.Cursor = Windows.Forms.Cursors.Default()
Application.DoEvents()
Dim mouseCur As Cursor
Me.Cursor = Windows.Forms.Cursors.WaitCursor
bkps.SQLBackup(oServer)
ProgressBar1.Value = 100
Application.DoEvents()
bkps = Nothing
Me.Cursor = Windows.Forms.Cursors.Default()
MsgBox("数据库恢复完成", MsgBoxStyle.Information, "系统消息")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "系统消息")
End Try
End Sub
Private Sub btnRestore_Click()Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
Try
Dim oServer As New SQLDMO.SQLServer
oServer.LoginSecure = False
If A.Checked Then
oServer.LoginSecure = True
oServer.Connect(cbServer.Text)
Else
oServer.Connect(cbServer.Text, txtUserName.Text, txtPassword.Text)
End If '连接服务器
Me.Cursor = Windows.Forms.Cursors.WaitCursor
rps = CreateObject("SQLDMO.Restore")
rps.Database = cbDatabase.Text '指定需备份的数据库
rps.Action = 0
rps.Files = "d:\" & cbDatabase.Text & ".bak" '指定备份文件
'rps.Initialize = True
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
Me.Cursor = Windows.Forms.Cursors.Default()
Application.DoEvents()
Dim mouseCur As Cursor
Me.Cursor = Windows.Forms.Cursors.WaitCursor
rps.SQLRestore(oServer)
ProgressBar1.Value = 100
Application.DoEvents()
rps = Nothing
Me.Cursor = Windows.Forms.Cursors.Default()
MsgBox("数据库备份完成", MsgBoxStyle.Information, "系统消息")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "系统消息")
End Try
End Sub
'显示进度
Private Sub bkps_PercentComplete()Sub bkps_PercentComplete(ByVal Message As String, ByVal Percent As Integer) Handles bkps.PercentComplete
ProgressBar1.Value = ProgressBar1.Maximum * (Percent / 100)
End Sub
Private Sub rps_PercentComplete()Sub rps_PercentComplete(ByVal Message As String, ByVal Percent As Integer) Handles rps.PercentComplete
ProgressBar1.Value = ProgressBar1.Maximum * (Percent / 100)
End Sub
End Class
下载源代码:/Files/duanzt/DBManager.rar
转载于:https://www.cnblogs.com/duanzt/archive/2006/09/12/501811.html