备份和还原SQL数据库

来源:互联网 发布:node vue webpack 编辑:程序博客网 时间:2024/04/30 11:20
    '过程名称:BackUpDataBase
    '功能描述:备份数据库
    '接收参数:需要备份的路径及文件名
    '返回参数:Ture,False 是否成功
    '创建人员及日期:zzz@2006-11-15
    Public Function BackUpDataBase(ByVal sDBName As StringAs Boolean
        
'生成备份SQL语句
        Dim backupSql As String, sComputerPath As String

        sComputerPath 
= SystemInformation.ComputerName

        
'sDBName = sDBName.Replace(":", "$")

        backupSql 
= "USE MASTER;"
        
'backupSql += "Backup DATABASE " & gDBName & " TO DISK='/" & sComputerPath & "" & sDBName & "';"
        backupSql += "Backup DATABASE " & gDBName & " TO DISK='" & sDBName & "';"

        
'定义SqlCommand对象并执行
        Dim myCommand As New SqlClient.SqlCommand
        
Dim sqlCon As New SqlClient.SqlConnection
        sqlCon.ConnectionString 
= SqlConnectString

        myCommand.CommandText 
= backupSql
        myCommand.Connection 
= sqlCon

        
Dim frmNewWait As New frmWaiting
        frmNewWait.Show()
        System.Windows.Forms.Application.DoEvents()
        
Try
            sqlCon.Open()
            myCommand.ExecuteNonQuery()
            BackUpDataBase 
= True
        
Catch ex As Exception
            
Throw New Exception("Error In ExecuteTransaction!!!(DataBaseClose)" & vbCrLf & _
                                                                  
"Source:" & ex.Source.ToString() + " Message:" + ex.Message.ToString())
            BackUpDataBase 
= False
        
Finally
            frmNewWait.Close()
            
If Not frmNewWait Is Nothing Then
                frmNewWait.Dispose()
            
End If

            sqlCon.Close()
            sqlCon.Dispose()
            myCommand.Dispose()
        
End Try
    
End Function


    
'过程名称:RestoreDataBase
    '功能描述:还原数据库
    '接收参数:需要还原的路径及文件名
    '返回参数:Ture,False 是否成功
    '创建人员及日期:zzz@2006-11-15
    Public Function RestoreDataBase(ByVal sDBName As StringAs Boolean
        
'生成SQL语句
        Dim RestoreSql As String
        RestoreSql 
= "USE MASTER;"

        
'定义SqlCommand对象并执行
        Dim myCommand As New SqlClient.SqlCommand
        
Dim sqlCon As New SqlClient.SqlConnection
        sqlCon.ConnectionString 
= SqlConnectString
        myCommand.Connection 
= sqlCon

        
Try
            sqlCon.Open()
            myCommand.CommandText 
= RestoreSql
            myCommand.ExecuteNonQuery()

            RestoreSql 
= "restore DATABASE " & gDBName & " FROM DISK='" & sDBName & "';"
            myCommand.CommandText 
= RestoreSql
            myCommand.ExecuteNonQuery()
            RestoreDataBase 
= True
        
Catch ex As Exception
            
Throw New Exception("Error In ExecuteTransaction!!!(DataBaseClose)" & vbCrLf & _
                                                                  
"Source:" & ex.Source.ToString() + " Message:" + ex.Message.ToString())
            RestoreDataBase 
= False
        
Finally
            sqlCon.Close()
            sqlCon.Dispose()
            myCommand.Dispose()
        
End Try
    
End Function

 
原创粉丝点击