用C#怎么样实现备份数据库与恢复数据库功能呀?

yangkang0752 2004-05-06 12:20:12
用C#怎么样实现备份数据库与恢复数据库功能呀?
...全文
84 9 点赞 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ppq886 2004-07-02
收藏
  • 打赏
  • 举报
回复
liqiang2003 2004-05-07
用存储过程:

1建立自动任务:
EXECUTE pr_AutoBackup "baktest","d:\Database",23,3
设置为每小时执行一次。
2.在Server – SQL Server – Configuration 中选中Auto Start Executive at Boot Time
3.在Master数据库建立存储过程
CREATE PROCEDURE pr_AutoBackup @db_to_bak varchar(20),
@file_path varchar(20),
@on_hour int,
@remain_day int
AS
DECLARE @this_day int,
@bak_count int,
@str_file_name varchar(30),
@str_del_device varchar(20),
@str_last_device varchar(20),
@last_day int,
@this_hour int,
@strSQL varchar(60)
SELECT @this_day = datepart(dayofyear,getdate())
SELECT @bak_count = count(name),@str_last_device = max(name),
@last_day = max(convert(int,substring(name,charindex("_",name)+1,3)))
FROM sysdevices
WHERE charindex("_",name)=datalength(@db_to_bak)+1
AND substring(name,1,datalength(@db_to_bak))=@db_to_bak
IF SUBSTRING(@file_path,datalength(@file_path),1)<>”\”
SELECT @file_path = @file_path + “\”
SELECT @this_hour = datepart(hour,getdate())
IF ((@this_hour = @on_hour)and(@this_day<>@last_day))or(@bak_count = 0)
BEGIN
SELECT @str_last_device = @db_to_bak+"_"+convert(varchar(3),@this_day)
SELECT @str_file_name = @file_path + @str_last_device + “.dat”
SELECT @strSQL =“DUMP TRANSACTION ” + @db_to_bak + “ WITH TRUNCATE_ONLY”
EXECUTE(@strSQL)
SELECT @strSQL =“sp_addumpdevice ‘DISK’,’”+@str_last_device + “’,’”+@str_file_name+”’”
EXECUTE(@strSQL)
SELECT @strSQL =“DUMP DATABASE “+ @db_to_bak + “ TO “ + @str_last_device
EXECUTE(@strSQL)
SELECT @str_del_device = @db_to_bak + ”_” + convert(varchar(4),@this_day - @remain_day)
SELECT @bak_count = count(name)
FROM sysdevices
WHERE name = @str_del_device
IF (@bak_count=1)
BEGIN
SELECT @strSQL =“sp_dropdevice “+ @str_del_device +”,DELFILE”
EXECUTE(@strSQL)
END
END
ELSE
BEGIN
SELECT @strSQL =“DUMP TRANSACTION “+ @db_to_bak + “ TO “ + @str_last_device
EXECUTE(@strSQL)
END
  • 打赏
  • 举报
回复
lg9706032921 2004-05-07
<HTML>
<HEAD>
<TITLE>SQL Server 数据库的备份与恢复</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
<form method="post" name=myform>
选择操作:<INPUT TYPE="radio" NAME="act" id="act_backup" value="backup"><label for=act_backup>备份</label> 
<INPUT TYPE="radio" NAME="act" id="act_restore" value="restore"><label for=act_restore>恢复</label>
<br>数据库名:<INPUT TYPE="text" NAME="databasename" value="<%=request("databasename")%>">
<br>文件路径:<INPUT TYPE="text" NAME="bak_file" value="c:\$1.bak">(备份或恢复的文件路径)<br>
<input type="submit" value="确定">
</form>
<%

dim sqlserver,sqlname,sqlpassword,sqlLoginTimeout,databasename,bak_file,act
sqlserver = "sa" 'sql服务器
sqlname = "sa" '用户名
sqlpassword = "123" '密码
sqlLoginTimeout = 15 '登陆超时
databasename = trim(request("databasename"))
bak_file = trim(request("bak_file"))
bak_file = replace(bak_file,"$1",databasename)
act = lcase(request("act"))
if databasename = "" then
response.write "input database name"
else
if act = "backup" then
Set srv=Server.CreateObject("SQLDMO.SQLServer")
srv.LoginTimeout = sqlLoginTimeout
srv.Connect sqlserver,sqlname, sqlpassword
Set bak = Server.CreateObject("SQLDMO.Backup")
bak.Database=databasename
bak.Devices=Files
bak.Files=bak_file
bak.SQLBackup srv
if err.number>0 then
response.write err.number&"<font color=red><br>"
response.write err.description&"</font>"
end if
Response.write "<font color=green>备份成功!</font>"
elseif act = "restore" then
'恢复时要在没有使用数据库时进行!
Set srv=Server.CreateObject("SQLDMO.SQLServer")
srv.LoginTimeout = sqlLoginTimeout
srv.Connect sqlserver,sqlname, sqlpassword
Set rest=Server.CreateObject("SQLDMO.Restore")
rest.Action=0 ' full db restore
rest.Database=databasename
rest.Devices=Files
rest.Files=bak_file
rest.ReplaceDatabase=True 'Force restore over existing database
if err.number>0 then
response.write err.number&"<font color=red><br>"
response.write err.description&"</font>"
end if
rest.SQLRestore srv

Response.write "<font color=green>恢复成功!</font>"
else
Response.write "<font color=red>没有选择操作</font>"
end if
end if
%>
</BODY>
</HTML>

用的SQLSERVER 自己的功能,
  • 打赏
  • 举报
回复
yangkang0752 2004-05-07
能够用存储过程来做吗?请大家帮帮忙吧,我在做一个系统,正要实现这个功能啊
  • 打赏
  • 举报
回复
ChinaKable 2004-05-06
关注,UP!
  • 打赏
  • 举报
回复
dongyp511 2004-05-06
关注,UP!

  • 打赏
  • 举报
回复
jaxkxu 2004-05-06
些批处理之后C#中调用 *.bat
  • 打赏
  • 举报
回复
mastercy 2004-05-06
建议把数据导出到另外的文件
恢复时在导入即可
  • 打赏
  • 举报
回复
李洪喜 2004-05-06
这方面的东东挺多的,搜索一下吧。
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-05-06 12:20
社区公告

让您成为最强悍的C#开发者