隨筆-17  評論-64  文章-79  trackbacks-1
          *********************************************************
          '* 名稱:BackupDatabase
          '* 功能:備份數(shù)據(jù)庫
          '* 控件:一個文本框和兩個按鈕(備份到和確定)
          '*********************************************************
          Public Sub BackupDatabase()
          Dim cn As New ADODB.Connection
          Dim s_path, s_dataexport As String
          s_path = App.Path
          Me.MousePointer = 11? ?'設置鼠標指針形狀
          'student1是需要備份的數(shù)據(jù)庫名稱
          s_dataexport = "backup database student1 to disk='" + CommonDialog1.FileName + "'"
          cn.Open "driver={sql server};server=" & d1 & ";database=student1;persist security info=false; userid=sa"??'數(shù)據(jù)庫連接字符串
          '這里不需要連接master數(shù)據(jù)庫,即可完成備份
          cn.BeginTrans
          cn.Execute s_dataexport
          Err.Number = 0
          If Err.Number = 0 Then
          ? ? cn.CommitTrans
          ? ? MsgBox "數(shù)據(jù)備份成功!", vbInformation, "提示"
          ? ? MsgBox "數(shù)據(jù)備份文件存放路徑:" & CommonDialog1.FileName, vbOKOnly, "提示"
          ? ? Unload Me
          Else
          ? ? cn.RollbackTrans
          ? ? MsgBox "數(shù)據(jù)備份失敗!請檢查數(shù)據(jù)庫是否正在打開!", vbCritical, "提示"
          End If
          cn.Close
          Set cn = Nothing
          Me.MousePointer = 1
          End Sub



          '*********************************************************
          '* 名稱:RestoreDataBase
          '* 功能:還原數(shù)據(jù)庫
          '* 控件:一個文本框和兩個按鈕( 打開和確定)
          '*********************************************************
          Public Sub RestoreDataBase()
          If Text1.Text = "" Then
          ? ? MsgBox "請選擇要恢復的數(shù)據(jù)文件!", vbInformation, "提示"
          ? ? Exit Sub
          Else
          ? ? ret = MsgBox("數(shù)據(jù)恢復操作將會覆蓋以前的所有數(shù)據(jù)并且覆蓋后無法恢復,您確定要進行恢復操作嗎?", 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數(shù)據(jù)庫才能完成數(shù)據(jù)恢復操作
          ? ?? ?? ?'同上student1為需要恢復的數(shù)據(jù)庫
          ? ?? ???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 "數(shù)據(jù)恢復成功!", vbInformation, "提示"
          ? ?? ?? ?? ?Command1.Enabled = True
          ? ?? ?? ?? ?Label1.Visible = False
          ? ?? ???Else
          ? ?? ?? ?? ?cn.RollbackTrans
          ? ?? ?? ?? ?MsgBox "數(shù)據(jù)恢復失敗!", 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? ?? ?? ?? ?? ?? ?? ? '''''''''''''''''''''''''''''''''''''''''''''
          posted on 2007-01-13 21:25 飛鳥 閱讀(1439) 評論(4)  編輯  收藏 所屬分類: VB

          評論:
          # re: VB實現(xiàn)SQL SERVER2000數(shù)據(jù)庫備份還原 2007-08-04 23:52 | li
          Dim DBC As New DataBaseConnection 是什么?d1、d2、d3是什么?  回復  更多評論
            
          # re: VB實現(xiàn)SQL SERVER2000數(shù)據(jù)庫備份還原 2007-08-06 12:56 | 飛鳥
          DataBaseConnection:數(shù)據(jù)庫連接對象
          d1:主機名(服務器名)
          d2:帳號
          d3:密碼

          總之這些都是要得到連接數(shù)據(jù)庫的字符串  回復  更多評論
            
          # re: VB實現(xiàn)SQL SERVER2000數(shù)據(jù)庫備份還原 2007-09-04 21:58 | 令狐沖(反正)我姓令
          哥們,真是太謝謝了,只是你那個恢復數(shù)據(jù)庫的Kill的存儲過程該怎樣寫?能發(fā)一下嗎????
            回復  更多評論
            
          # re: VB實現(xiàn)SQL SERVER2000數(shù)據(jù)庫備份還原 2007-09-05 13:01 | 飛鳥
          kill是系統(tǒng)自帶,所以不需要寫  回復  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 吉林市| 秦皇岛市| 中阳县| 开封市| 尼勒克县| 綦江县| 台东市| 慈溪市| 都江堰市| 新源县| 拉萨市| 杭锦后旗| 高台县| 楚雄市| 盈江县| 雷山县| 昂仁县| 梅州市| 贡嘎县| 罗田县| 宜丰县| 鲜城| 洛隆县| 冷水江市| 柳林县| 阆中市| 朝阳市| 海晏县| 兰西县| 道孚县| 连南| 广德县| 沐川县| 尼勒克县| 福建省| 柘城县| 武义县| 滁州市| 寿宁县| 青冈县| 泰兴市|