Dim DBC As New DataBaseConnection
If db.State = 1 Then
db.Close
End If
db.ConnectionString = DBC.SqlConnectString(d1, d2, d3)
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
db.CursorLocation = adUseClient
db.Open
上面的db,d1,d2,d3,rs是什么,值是什么
备份:
Public Sub DoBackup(byval strPathFile As String)
On Error GoTo ER
Dim oMouse As clsMouseCtrl
Dim CN As ADODB.Connection
Dim strDatabase As String
Dim strSql As String
Dim rs As ADODB.Recordset
Dim i As Long
Dim str As String
Dim blnPathIsExist As Boolean
Set oTool = New sTool
Set oRule = New bRuleOnCompany
strSql = "master.dbo.xp_cmdshell 'dir " & strPathInRule & "'"
Set rs = New ADODB.Recordset
rs.Open strSql, gCON, adOpenDynamic, adLockReadOnly
blnPathIsExist = False
For i = 0 To rs.RecordCount - 1
str = Change2Str(rs.Fields("OUTPUT").Value)
If str <> "" Then
If InStr(1, str, strPathInRule, vbTextCompare) > 0 Then
blnPathIsExist = True
Exit For
End If
End If
rs.MoveNext
Next i
If blnPathIsExist = False Then
strSql = "master.dbo.xp_cmdshell 'mkdir " & strPathInRule & "'"
gCON.Execute strSql
End If
If strPathFile <> "" Then
Set CN = gCON
strDatabase = oTool.Dbname
Else
Exit Sub
End If
DoEvents
With CN
strSql = "BackUp Database " & strDatabase & " to disk='" & strPathFile & "'"
CN.Execute strSql
End With
Exit Sub
ER:
MsgBox "Backup Failed!" & vbCrLf & Err.Description '"Backup Failed!"
End Sub
恢复:
Public Function DoRestore(ByVal strServer As String, ByVal strDatabase As String, _
ByVal strUser As String, ByVal strPasswd As String, ByVal strPathFile As String, _
ByVal astrLogicalFiles As Variant, ByVal astrPhysicalFiles As Variant, _
ByVal blnReplace As Boolean, Optional ByVal blnSecurityIntegrator As Boolean = False) As String
Dim oMouse As clsMouseCtrl
Set oMouse = New clsMouseCtrl
DoEvents
On Error GoTo ER
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
With cn
.CursorLocation = adUseClient
.CommandTimeout = 0
Dim strSql As String
Dim i As Integer
strSql = "RESTORE DATABASE " & strDatabase & " FROM DISK = '" & strPathFile & _
"' WITH RECOVERY" & IIf(blnReplace = True, ",REPLACE", "")
For i = 1 To SafeUBound(astrLogicalFiles)
strSql = strSql & ",move '" & astrLogicalFiles(i) & "' to '" & astrPhysicalFiles(i) & "'"
Next i
cn.Execute strSql
DoRestore = ""
End With
Exit Function
ER:
DoRestore = Err.Description
End Function
'*********************************************************
'* 名称:RestoreDataBase
'* 功能:还原数据库
'* 控件:一个文本框和两个按钮( 打开和确定)
'*********************************************************
Public Sub RestoreDataBase()
If Text1.Text = "" Then
MsgBox "请选择要恢复的数据文件!", vbInformation, "提示"
Exit Sub
Else
ret = MsgBox("数据恢复操作将会覆盖以前的所有数据并且覆盖后无法恢复,您确定要进行恢复操作吗?", vbQuestion + vbOKCancel, "提示")
If ret = vbOK Then
Dim cn As New ADODB.Connection
Dim sn As New ADODB.Recordset
Dim s_restore As String
Me.MousePointer = 11
cn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;server=" & d1 & ";Initial Catalog=master;Data Source=127.0.0.1;user id=sa;password=" & d3 & ""
sn.Open "select spid from sysprocesses where dbid=db_id('student1')", cn
Do While Not sn.EOF
cn.Execute "kill " & sn("spid")
sn.MoveNext
Loop
sn.Close
s_restore = "restore database student1 from disk='" + Trim(Text1.Text) + "' with REPLACE"
cn.Execute s_restore
'Debug.Print gs_conn_string
'此时需要连接master数据库才能完成数据恢复操作
'同上student1为需要恢复的数据库
s_restore = "restore database student1 from disk='" + Trim(Text1.Text) + "'"
'text1一个用于记录需要恢复文件的地址的textbox
cn.Execute s_restore
cn.BeginTrans
If Err.Number = 0 Then
cn.CommitTrans
MsgBox "数据恢复成功!", vbInformation, "提示"
Command1.Enabled = True
Label1.Visible = False
Else
cn.RollbackTrans
MsgBox "数据恢复失败!", vbCritical, "提示"
Command1.Enabled = True
End If
cn.Close
Set cn = Nothing
Me.MousePointer = 1
Else
Exit Sub
End If '''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
Dim DBC As New DataBaseConnection
If db.State = 1 Then
db.Close
End If
db.ConnectionString = DBC.SqlConnectString(d1, d2, d3)
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
db.CursorLocation = adUseClient
db.Open
Set cmd.ActiveConnection = db
If Err.Number Then
MsgBox Err.Description, 16 + vbOKOnly, Err.Number
Exit Sub
End If
db.DefaultDatabase = "student1"
If Err.Number Then
MsgBox Err.Description, 16 + vbOKOnly, Err.Number
Exit Sub
End If
End If
End Sub '''''''''''''''''''''''''''''''''''''''''''''
'得到还原后的数据库存放目录,如果没有指定,存放到SQL SERVER的DATA目录
If sDataBasePath = "" Then
iSql = "select filename from master..sysfiles"
iRe.Open iSql, iDb, adOpenKeyset, adLockReadOnly
iSql = iRe(0)
iRe.Close
sDataBasePath = Left(iSql, InStrRev(iSql, "\"))
End If
'检查数据库是否存在
If sReplaceExist = False Then
iSql = "select 1 from master..sysdatabases where name='" & sDataBaseName & "'"
iRe.Open iSql, iDb, adOpenKeyset, adLockReadOnly
If iRe.EOF = False Then
iReturn = "数据库已经存在!"
iRe.Close
GoTo lbExit
End If
iRe.Close
End If