'备份系统文件
Public Shared Sub BackupSystemFile()
Dim filepath As String
filepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly.Location)
Try
'备份数据库
CopyFile(filepath & "\infocenter.mdb", filepath & "\backup\infocenter.mdb")
Catch ex As Exception
Throw New Exception("备份数据时失败," & ex.Message)
End Try
End Sub
'复制文件,如文件在目的地已存在,则删除之
Private Shared Sub CopyFile(ByVal sourceFileName As String, ByVal DestFileName As String)
Try
'删除已有的文件
If Dir(DestFileName) <> String.Empty Then
System.IO.File.Delete(DestFileName)
End If
System.IO.File.Copy(sourceFileName, DestFileName)
Catch ex As Exception
Throw New Exception("操作文件:" & sourceFileName & "---" & DestFileName & "时失败,系统给出的信息为:" & ex.Message)
End Try
End Sub
'复原系统文件
Public Shared Sub RestoreSystemFile()
Dim filepath As String
filepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly.Location)
Try
'复原配置文件
CopyFile(filepath & "\backup\PersonalInfo.cfg", filepath & "\personalinfo.cfg")
Catch ex As Exception
Throw New Exception("还原数据时失败," & ex.Message)
End Try
我写的备份的存储过程代码:
CREATE PROCEDURE pr_backup_db
@flag int out ,
@backup_db_name varchar(128),
@filename varchar(1000)
as
declare @sql nvarchar(4000),@par nvarchar(1000)
if not exists(select * from master..sysdatabases where name=@backup_db_name)
begin
select @flag=1--'<Parameter>DB Not Found'
return @flag
end
else
begin
if right(@filename,1)<>'\' and charindex('\',@filename)<>0
begin
select @par='@filename varchar(1000)'
select @sql='BACKUP DATABASE '+ @backup_db_name +' to disk=@filename with init'
execute sp_executesql @sql,@par,@filename
if @@ERROR = 0
begin
select @flag=9--'<Backup>Backup Success'
return @flag
end
else
begin
select @flag=2--'<Backup>File Path Error'
return @flag
end
end
else
begin
select @flag=3--'<Parameter>File Name Error'
return
end
end
GO
'数据库备份
Dim conn As SqlConnection = New SqlConnection("server =(local) ";database=数据库名;user id=sa;password=;")
Dim objdataadapter As New SqlDataAdapter
Dim objdataset As DataSet = New DataSet
objdataadapter.SelectCommand = New SqlCommand
objdataadapter.SelectCommand.Connection = conn
objdataadapter.SelectCommand.CommandText = "backup database 数据库名 to disk='备份路径+文件名'"
Try
conn.Open()
objdataadapter.SelectCommand.ExecuteNonQuery()
conn.close()
MessageBox.Show("备份数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'数据库恢复
Dim conn As SqlConnection = New SqlConnection("server =(local);database=数据库名;user id=用户名;password=密码;")
Dim objdataadapter As New SqlDataAdapter
Dim objdataset As DataSet = New DataSet
objdataadapter.SelectCommand = New SqlCommand
objdataadapter.SelectCommand.Connection = conn
objdataadapter.SelectCommand.CommandText = "restore database 数据库名 from disk='要恢复的数据库路径'"
Try
conn.Open()
objdataadapter.SelectCommand.ExecuteNonQuery()
conn.Close()
MessageBox.Show("数据恢复成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
Dim result As DialogResult
result = MessageBox.Show(ex.Message)
If result = DialogResult.OK Then
Exit Try
End If
End Try