Screen.MousePointer = 11
Dim oSvr As SQLDMO.SQLServer
On Error Resume Next
Set oSvr = CreateObject("SQLDMO.SQLServer")
If Err > 0 Then
If Err = 429 Then
MsgBox "请首先安装 MSDE": Screen.MousePointer = 0: Exit Sub
End If
End If
oSvr.LoginTimeout = 60 ' 这个值要大一些以免产生逾时错误.
oSvr.Start True, "(local)", "sa", ""
oSvr.Stop
Screen.MousePointer = 0
MsgBox "成功"注意:引用中应有Microsoft SQLDMO Object Library
Dim oSvr As SQLDMO.SQLServer
On Error Resume Next
Set oSvr = CreateObject("SQLDMO.SQLServer")
If Err > 0 Then
If Err = 429 Then
MsgBox "请首先安装 MSDE": Screen.MousePointer = 0: Exit Sub
End If
End If
oSvr.LoginTimeout = 60 ' 这个值要大一些以免产生逾时错误.
oSvr.Start True, "(local)", "sa", ""
oSvr.Stop
Screen.MousePointer = 0
MsgBox "成功"注意:引用中应有Microsoft SQLDMO Object Library
我做过了
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form frmbackup
Caption = "备份"
ClientHeight = 3540
ClientLeft = 3015
ClientTop = 2490
ClientWidth = 4890
ControlBox = 0 'False
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 3540
ScaleWidth = 4890
Begin MSComctlLib.ProgressBar ProgressBar1
Height = 255
Left = 1440
TabIndex = 6
Top = 1440
Width = 1815
_ExtentX = 3201
_ExtentY = 450
_Version = 393216
Appearance = 1
Min = 5
End
Begin VB.Timer Timer1
Enabled = 0 'False
Interval = 1000
Left = 4200
Top = 3000
End
Begin VB.CommandButton Command2
Caption = "退出"
Height = 375
Left = 1680
TabIndex = 1
Top = 2760
Width = 975
End
Begin VB.CommandButton Command1
Caption = "备份"
Height = 375
Left = 1680
TabIndex = 0
Top = 2040
Width = 975
End
Begin VB.Label Label4
Caption = "数据库备份成功请退出"
Height = 495
Left = 1440
TabIndex = 5
Top = 1200
Visible = 0 'False
Width = 1815
End
Begin VB.Label Label3
Caption = "秒"
Height = 255
Left = 1440
TabIndex = 4
Top = 720
Width = 255
End
Begin VB.Label Label2
Caption = "5"
Height = 255
Left = 1320
TabIndex = 3
Top = 720
Width = 255
End
Begin VB.Label Label1
Caption = "剩余时间:"
Height = 255
Left = 360
TabIndex = 2
Top = 720
Width = 975
End
End
Attribute VB_Name = "frmbackup"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim x As Integer
Dim y As Integer
Private Sub Command1_Click()
Dim sql As String
sql = "backup database cargps to disk= 'e:\backup\cargps'"
Set rs = cn.Execute(sql)
'Set rs = Nothing
Timer1.Enabled = True
Command1.Enabled = False
End SubPrivate Sub Command2_Click()
Unload Me
'Set rs = Nothing
FrmMain.Show
End SubPrivate Sub Form_Load()
x = 5
y = 0
End SubPrivate Sub Label5_Click()
End SubPrivate Sub Timer1_Timer()
x = x - 1
y = y + 20ProgressBar1.Value = y
Label2.Caption = x
If x = 0 Then
Timer1.Enabled = False
Label1.Visible = False
Label2.Visible = False
Label3.Visible = False
Label4.Visible = True
End IfEnd Sub
Dim sql As String
sql = "backup database cargps to disk= 'e:\backup\cargps'"
Set rs = cn.Execute(sql)
'Set rs = Nothing
Timer1.Enabled = True
Command1.Enabled = False
End Sub上面的加了一个TIMER 和 BAR显示备份的进度而已
把窗体发一份给我
“cargps”,哈哈哈
你备份的时候连接master数据库就行了!偶就是这样做的!
如果你要代码的话,发短消息给我!
星期一到公司发给你!
把窗体发一份给我
“[email protected]”,哈哈哈
Dim cn As New ADODB.Connection
Dim s_path, s_dataexport As String
s_path = App.path
Me.MousePointer = 11
'下面的语句即为备份的语句
’其中:wzgl是需要备份的数据库名称
s_dataexport = "backup database wzgl to disk='c:\backup.dat'"
cn.Open 数据库连接字符串
'这里不需要连接master数据库,即可完成备份
cn.BeginTrans
cn.Execute s_dataexport
Err.Number = 0
If Err.Number = 0 Then
cn.CommitTrans
MsgBox "数据备份成功!", vbInformation, "提示"
Else
cn.RollbackTrans
MsgBox "数据备份失败!", vbCritical, "提示"
End If
cn.Close
Set cn = Nothing
Me.MousePointer = 1
恢复:
Dim ret As Integer
If Text1.Text = "" Then
MsgBox "请选择要恢复的数据文件!", vbInformation, "提示"
Exit Sub
Else
ret = MsgBox("数据恢复操作将会覆盖以前的所有数据并且覆盖后无法恢复,您确定要进行恢复操作吗?", vbQuestion + vbOKCancel + "提示")
If ret = vbOK Then
Me.MousePointer = 11
Dim cn As New ADODB.Connection
Dim s_restore As String
'Debug.Print gs_conn_string
'此时需要连接master数据库才能完成数据恢复操作
cn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=127.0.0.1 "
'同上wzgl为需要恢复的数据库
s_restore = "restore database wzgl from disk='" + Trim(Text1.Text) + "'"
'text1一个用于记录需要恢复文件的地址的textbox
cn.Execute s_restore
cn.BeginTrans
If Err.Number = 0 Then
cn.CommitTrans
MsgBox "数据恢复成功!", vbInformation, "提示"
Else
cn.RollbackTrans
MsgBox "数据恢复失败!", vbCritical, "提示"
End If
cn.Close
Set cn = Nothing
Me.MousePointer = 1
Else
Exit Sub
End If
End If
拷完文件后,用 sp_attach_db '数据库名','物理文件名','物理日志文件名' 连接上数据库