备份和还原SQL数据库

     ' 过程名称: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

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值