⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 简单的备份和恢复SQL SERER数据库的代码
💻 FRM
字号:
VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{46DBBAE5-ED3E-4D0A-BC4E-8031490B83C7}#1.0#0"; "ciaXPProgress30.ocx"
Object = "{88F7F54F-F24B-4B64-B0E0-2454E1E6DA40}#1.0#0"; "ciaXPButton30.ocx"
Object = "{268F1C9C-A0AF-4B47-BB71-8D9162E3480C}#1.0#0"; "ciaXPText30.ocx"
Begin VB.Form frmbackup 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "数据库备份&还原"
   ClientHeight    =   1350
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   7005
   Icon            =   "Form1.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   1350
   ScaleWidth      =   7005
   StartUpPosition =   2  'CenterScreen
   Begin ciaXPButton30.XPButton30 CmdRestore 
      Height          =   375
      Left            =   3300
      TabIndex        =   4
      Top             =   660
      Width           =   1335
      _ExtentX        =   2355
      _ExtentY        =   661
      AutoSelectTheme =   -1  'True
      Caption         =   "8FD8539F6570636E5E93"
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      UseImageShadow  =   0   'False
      LicValid        =   -1  'True
   End
   Begin ciaXPButton30.XPButton30 CmdBackup 
      Height          =   375
      Left            =   1440
      TabIndex        =   3
      Top             =   660
      Width           =   1335
      _ExtentX        =   2355
      _ExtentY        =   661
      AutoSelectTheme =   -1  'True
      Caption         =   "59074EFD6570636E5E93"
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      UseImageShadow  =   0   'False
      LicValid        =   -1  'True
   End
   Begin ciaXPButton30.XPButton30 CmdView 
      Height          =   375
      Left            =   5460
      TabIndex        =   2
      Top             =   150
      Width           =   1335
      _ExtentX        =   2355
      _ExtentY        =   661
      AutoSelectTheme =   -1  'True
      Caption         =   "900962E94FDD5B588DEF5F84"
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      UseImageShadow  =   0   'False
      LicValid        =   -1  'True
   End
   Begin ciaXPText30.XPText30 Text1 
      Height          =   315
      Left            =   120
      TabIndex        =   1
      Top             =   180
      Width           =   5235
      _ExtentX        =   9234
      _ExtentY        =   556
      BeginProperty ButtonFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      LicValid        =   -1  'True
   End
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   100
      Left            =   6240
      Top             =   540
   End
   Begin ciaXPProgress30.ProgressBar30 pBar 
      Height          =   195
      Left            =   120
      TabIndex        =   0
      Top             =   1080
      Visible         =   0   'False
      Width           =   6735
      _ExtentX        =   11880
      _ExtentY        =   344
      LicValid        =   -1  'True
   End
   Begin MSComDlg.CommonDialog CommonDialog1 
      Left            =   5520
      Top             =   480
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
End
Attribute VB_Name = "frmbackup"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim tablename() As String
Dim savefilename As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Private Sub CmdView_Click()
CommonDialog1.DefaultExt = "*.mdb"
CommonDialog1.Filter = "数据库文件(*.back)|*.back"
CommonDialog1.InitDir = App.Path & "\data"
CommonDialog1.DialogTitle = "选择保存位置"
CommonDialog1.ShowSave
If CommonDialog1.FileName = "" Or Err.Number <> 0 Then Exit Sub
Text1 = CommonDialog1.FileName
savefilename = CommonDialog1.FileName
End Sub

Private Sub CmdBackup_Click()
Dim i As Long, j As Long
If Len(savefilename) = 0 Then MsgBox "请选择保存路径!", vbCritical: Exit Sub
CmdEnable False
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestAI;Data Source=."
cn.Execute "backup database TestAI to disk='" & savefilename & "'"
Set rs = New ADODB.Recordset
rs.Open "select * from dbo.sysobjects where type='U' and id <> object_id(N'[dbo].[BeginCollectRb]')" & _
                                "and id <> object_id(N'[dbo].[Cycle_MainSaveFileName]')" & _
                                "and id <> object_id(N'[dbo].[CyCle_SlaveSaveFileName]')" & _
                                "and id <> object_id(N'[dbo].[LoginUser]')" & _
                                "and id <> object_id(N'[dbo].[RB_Address]')" & _
                                "and id <> object_id(N'[dbo].[RB_OriginalData]')" & _
                                "and id <> object_id(N'[dbo].[RB_SaveFileName]')" & _
                                "and id <> object_id(N'[dbo].[Stove_OriginalData]')" & _
                                "and id <> object_id(N'[dbo].[Stove_SaveFileName]')" & _
                                 "and OBJECTPROPERTY(id, N'IsUserTable') = 1 ", cn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
  rs.MoveFirst
  For i = 0 To rs.RecordCount - 1
    If InStr(1, rs.Fields("name"), "dtproperties") = 0 Then
        cn.Execute "drop table dbo.[" & rs.Fields("name") & "]"
    End If
    rs.MoveNext
  Next
End If
If Err.Number = 0 Then MsgBox "数据库备份成功,保存路径: " & savefilename, vbInformation
CmdEnable True
End Sub

Private Sub CmdRestore_Click()
On Error GoTo RestoreErr
If Len(savefilename) = 0 Then MsgBox "请选择保存路径!", vbCritical: Exit Sub
Timer1.Enabled = True
pBar.Visible = True
CmdEnable False
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=."
cn.Execute "RESTORE DATABASE TestAI from disk='" & savefilename & "' WITH REPLACE,MOVE 'TestAI_Data' TO 'D:\Microsoft SQL Server\MSSQL\Data\TestAI_Data.mdf',MOVE 'TestAI_log' TO 'D:\Microsoft SQL Server\MSSQL\Data\TestAI.ldf' "
Exit Sub
RestoreErr:
  Timer1.Enabled = False
  pBar.Visible = False
  MsgBox "请确定Sql Server 企业管理器以关闭!", vbCritical
  CmdEnable True
End Sub

Private Sub Timer1_Timer()
If pBar.Value + 1 > 100 Then
    pBar.Value = 100
    Timer1.Enabled = False
    MsgBox "还原数据库成功!", vbInformation
    pBar.Visible = False
    CmdEnable True
Else
    pBar.Value = pBar.Value + 1
End If
End Sub

Private Sub CmdEnable(flag As Boolean)
CmdBackup.Enabled = flag
CmdRestore.Enabled = flag
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -