VB.NET DMO SQL SERVER备份恢复

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写处置以清理组件列表。
Protected Overloads Overrides 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 ProgressBar1 As System.Windows.Forms.ProgressBar
Friend WithEvents Button1 As System.Windows.Forms.Button

  1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
  2Me.ProgressBar1 = New System.Windows.Forms.ProgressBar()   
  3Me.Button1 = New System.Windows.Forms.Button()   
  4Me.SuspendLayout()   
  5'   
  6'ProgressBar1   
  7'   
  8Me.ProgressBar1.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _   
  9Or System.Windows.Forms.AnchorStyles.Right)   
 10Me.ProgressBar1.Location = New System.Drawing.Point(8, 8)   
 11Me.ProgressBar1.Name = "ProgressBar1"   
 12Me.ProgressBar1.Size = New System.Drawing.Size(384, 23)   
 13Me.ProgressBar1.TabIndex = 0   
 14'   
 15'Button1   
 16'   
 17Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat   
 18Me.Button1.Location = New System.Drawing.Point(160, 40)   
 19Me.Button1.Name = "Button1"   
 20Me.Button1.Size = New System.Drawing.Size(75, 24)   
 21Me.Button1.TabIndex = 1   
 22Me.Button1.Text = "备份"   
 23'   
 24'Form1   
 25'   
 26Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)   
 27Me.BackColor = System.Drawing.Color.CornflowerBlue   
 28Me.ClientSize = New System.Drawing.Size(400, 74)   
 29Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.ProgressBar1})   
 30Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow   
 31Me.MaximizeBox = False   
 32Me.MinimizeBox = False   
 33Me.Name = "Form1"   
 34Me.ShowInTaskbar = False   
 35Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen   
 36Me.ResumeLayout(False) 
 37
 38End Sub 
 39
 40#End Region 
 41
 42'此处由于嫌麻烦未按属性来处理……应改进   
 43Public sa As String   
 44Public passerd As String   
 45Public file As String   
 46Public database As String   
 47Public sqlserver As String 
 48
 49Public flag As Boolean = True 
 50
 51  
 52Private WithEvents PBackup As New SQLDMO.Backup()   
 53Private WithEvents oRestore As New SQLDMO.Restore() 
 54
 55' Property SQLServerName() As String   
 56' Get   
 57' Return sqlserver   
 58' End Get   
 59' Set(ByVal Value As String)   
 60' sqlserver = Value   
 61' End Set   
 62' End Property 
 63
 64  
 65'备份   
 66Public Function BACKUP(ByVal DATABASENAME As String, ByVal SQLServe As String, ByVal ROLE As String, ByVal PASSWORD As String, ByVal backfilename As String) 
 67
 68  
 69Me.ProgressBar1.Value = 0   
 70Me.BackColor = System.Drawing.Color.RoyalBlue   
 71Me.Button1.Text = "备份"   
 72Me.Show()   
 73On Error GoTo ErrHandler 
 74
 75Dim SQLSER As New SQLDMO.SQLServer 
 76
 77SQLSER.Connect(SQLServe, ROLE, PASSWORD)   
 78PBackup.Database = DATABASENAME 
 79
 80System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor 
 81
 82PBackup.Files = backfilename   
 83PBackup.SQLBackup(SQLSER) 
 84
 85SQLSER.DisConnect()   
 86SQLSER = Nothing   
 87System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default   
 88Exit Function 
 89
 90ErrHandler: 
 91
 92MsgBox("备份失败,请检查源数据库是否存在、角色权限是否足够或SQL SERVER服务是否被打开" &amp; Chr(13) &amp; "信息:" &amp; Err.Description)   
 93Resume Next   
 94End Function 
 95
 96'恢复   
 97Public Function RSTORE(ByVal DATABASENAME As String, ByVal SQLServe As String, ByVal ROLE As String, ByVal PASSWORD As String, ByVal restfilename As String) 
 98
 99Me.ProgressBar1.Value = 0 
100
101Me.BackColor = System.Drawing.Color.OliveDrab   
102Me.Button1.Text = "恢复"   
103Me.Show()   
104On Error GoTo ErrHandler 
105
106  
107Dim SQLSER As New SQLDMO.SQLServer 
108
109SQLSER.Connect(SQLServe, ROLE, PASSWORD) 
110
111oRestore.Database = DATABASENAME   
112oRestore.Files = restfilename 
113
114  
115System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor   
116oRestore.SQLRestore(SQLSER)   
117System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default 
118
119SQLSER.DisConnect()   
120SQLSER = Nothing 
121
122Exit Function 
123
124ErrHandler:   
125MsgBox("恢复失败,请检查恢复数据库是否存在、角色权限是否足够或SQL SERVER服务是否被打开" &amp; Chr(13) &amp; "信息:" &amp; Err.Description)   
126Resume Next   
127End Function 
128
129  
130Private Sub PBackup_Complete(ByVal Message As String) Handles PBackup.Complete 
131
132Me.ProgressBar1.Value = 100   
133PBackup = Nothing   
134Me.oRestore = Nothing   
135Me.Close() 
136
137End Sub   
138Private Sub PBackup_PercentComplete(ByVal Message As String, ByVal Percent As Integer) Handles PBackup.PercentComplete   
139Me.BackColor = System.Drawing.Color.RoyalBlue 
140
141Me.ProgressBar1.Value = Percent   
142If Percent = 100 Then 
143
144Me.ProgressBar1.Value = 100   
145PBackup = Nothing   
146Me.oRestore = Nothing   
147Me.Close() 
148
149End If   
150End Sub   
151Private Sub oRestore_Complete(ByVal Message As String) Handles oRestore.Complete 
152
153Me.ProgressBar1.Value = 100   
154PBackup = Nothing   
155Me.oRestore = Nothing   
156Me.Close()   
157End Sub   
158Private Sub oRestore_PercentComplete(ByVal Message As String, ByVal Percent As Integer) Handles oRestore.PercentComplete   
159Me.BackColor = System.Drawing.Color.OliveDrab 
160
161Me.ProgressBar1.Value = Percent   
162If Percent = 100 Then 
163
164Me.ProgressBar1.Value = 100   
165PBackup = Nothing   
166Me.oRestore = Nothing   
167Me.Close()   
168End If   
169End Sub 
170
171Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
172If flag = True Then 
173
174Me.BACKUP(Me.database, Me.sqlserver, Me.sa, Me.passerd, Me.file) 
175
176Else   
177Me.RSTORE(Me.database, Me.sqlserver, Me.sa, Me.passerd, Me.file) 
178
179End If 
180
181End Sub 
182
183  
184End Class</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus